How to create a dictionary from another dictionary in python

What is the best way to create a dict, with some attributes, from another dict, in Python?

For example, suppose I have the following dict:

dict1 = {
    name:          'Jaime',
    last_name:     'Rivera',
    phone_number:  '111111',
    email:         '',
    password :     'xxxxxxx',
    token:         'xxxxxxx',
    secret_stuff:  'yyyyyyy'   
}

I'd like to obtain

dict2 = {
    name:          'Jaime',
    last_name:     'Rivera',
    phone_number:  '111111',
    email:         ''
}

nbro

14.3k27 gold badges104 silver badges188 bronze badges

asked Aug 24, 2012 at 21:46

Jaime RiveraJaime Rivera

1,2645 gold badges17 silver badges27 bronze badges

0

For instance:

keys = ['name', 'last_name', 'phone_number', 'email']
dict2 = {x:dict1[x] for x in keys}

answered Aug 24, 2012 at 21:48

Lev LevitskyLev Levitsky

61.1k20 gold badges142 silver badges170 bronze badges

5

Using dict comprehension:

required_fields = ['name', 'last_name', 'phone_number', 'email']
dict2 = {key:value for key, value in dict1.items[] if key in required_fields}

answered Aug 24, 2012 at 21:49

Rostyslav DzinkoRostyslav Dzinko

38.5k5 gold badges49 silver badges60 bronze badges

for key in d1:
    if key in wanted_keys:
        d2[key] = d1[key]

update

I recently figured out that there's a much cleaner way of doing that with dict comprehensions

wanted_keys = set[['this_key', 'that_key']]
new_dict = {k: d1[k] for k in d1.keys[] & wanted_keys}

answered Aug 24, 2012 at 21:49

For anyone wanting a complete copy of dict1 without making a list of keys:

dict2 = {k: v for k, v in dict1.items[]}

LundinCast

9,0144 gold badges36 silver badges47 bronze badges

answered Jan 5, 2019 at 16:05

DanDan

871 silver badge1 bronze badge

4

Not sure how anyone didn't answer this.

You can use double asterisk to deconstruct a dict, and that co-incidentally happens to work inside another dict too.

someweirddata = {"hello":"world"}
print[{"initial data":"yati yati yata",**someweirddata}]

output:

{'initial data': 'yati yati yata', 'hello': 'world'}

This is the same as Javascript's ..., except that it's **

answered Feb 11, 2021 at 20:00

1

def removekey[mydict, key]:
    r = dict[mydict]
    del r[key]
    return r

pass mydict dictionary and key to be deleted, returns the remaining dictionary

answered Aug 24, 2012 at 21:49

amitchhajeramitchhajer

12.1k6 gold badges39 silver badges52 bronze badges

Can I make a dictionary of dictionaries in Python?

In Python, a nested dictionary is a dictionary inside a dictionary. It's a collection of dictionaries into one single dictionary. Here, the nested_dict is a nested dictionary with the dictionary dictA and dictB . They are two dictionary each having own key and value.

How can you copy the entire dictionary to a new dictionary Python?

How to copy a dictionary in Python.
Use a for loop. A for loop can be used to iterate through the original dictionary and copy the elements into a new, empty dictionary one by one. ... .
Use copy[] This is a built-in Python function that can be used to create a shallow copy of a dictionary. ... .
Use dict[].

How can you copy the entire dictionary to a new dictionary?

The dict. copy[] method returns a shallow copy of the dictionary. The dictionary can also be copied using the = operator, which points to the same object as the original. So if any change is made in the copied dictionary will also reflect in the original dictionary.

Can a dictionary be a key in another dictionary Python?

A dictionary or a list cannot be a key. Values, on the other hand, can literally be anything and they can be used more than once.

Chủ Đề