Why am I getting "None" after merging two dictionaries [duplicate] - python

This question already has answers here:
Why doesn't a python dict.update() return the object?
(11 answers)
Closed 6 years ago.
I have the following Python script where I merge two dictionaries:
dict1 = {'bookA': 1, 'bookB': 2, 'bookC': 3}
dict2 = {'bookC': 2, 'bookD': 4, 'bookE': 5}
print dict2.update(dict1)
Why do I get as output None rather than the merged dictionaries? How can I display the result?
Thanks.

update does not return a new dictionary.
Do this instead:
dict1 = {'bookA': 1, 'bookB': 2, 'bookC': 3}
dict2 = {'bookC': 2, 'bookD': 4, 'bookE': 5}
dict2.update(dict1)
print(dict2)

dict2.update(dict1) updates dict2, but doesn't return it. Use print dict2 instead.

Related

How to create a list of dicts into a single dict with python? [duplicate]

This question already has answers here:
How do I merge a list of dicts into a single dict?
(11 answers)
How do I merge dictionaries together in Python?
(7 answers)
Closed 1 year ago.
I have a large list of dictionaries, each with exactly one entry and with unique keys, and I want to 'combine' them into a single dict with python 3.8.
So here is an example that actually works:
mylist = [{'a':1}, {'b':2}, {'c':3}]
mydict = {list(x.keys())[0]:list(x.values())[0] for x in mylist}
which gives as result the expected output:
{'a': 1, 'b': 2, 'c': 3}
But it looks ugly and not quite pythonic. Is there a better one-line solution to this problem?
This is similar to the question asked HERE, but in my example I am looking for an answer (1) to merge many dicts together and (2) for a one-line solution. That makes my question different from the question already asked.
mydict = { k:v for elt in mylist for k, v in elt.items()}
Try this out, simple and effective.
mylist = [{'a':1}, {'b':2}, {'c':3}]
result = {}
for d in mylist:
result.update(d)
Result
{'a': 1, 'b': 2, 'c': 3}

Merge dictionaries in list python [duplicate]

This question already has an answer here:
Merging list of dicts in python
(1 answer)
Closed 2 years ago.
I have the a list of dicts in python in the following format:
dict1 = [{'Name':'a', 'value':20},{'Name':'b', 'value':10},{'Name':'c', 'value':15}]
I want to output something like this:
dict2 = {'a':20, 'b':10, 'c':15 }
How to do it? Thanks in advance!
Try with a dictionary comprehension:
dict1 = [{'Name':'a', 'value':20},{'Name':'b', 'value':10},{'Name':'c', 'value':15}]
dict2={dc['Name']:dc['value'] for dc in dict1}
Output:
dict2
{'a': 20, 'b': 10, 'c': 15}
I think you can do it with for loop efficiently. Check this:
dict1 = [{'Name':'a', 'value':20},{'Name':'b', 'value':10},{'Name':'c', 'value':15}]
dict2 = dict()
for a in range(len(dict1)):
dict2[dict1[a].get('Name')] = dict1[a].get('value')
print(dict2)
Output:
{'a': 20, 'b': 10, 'c': 15}

Append elements from external dict into dict inside assignment [duplicate]

This question already has answers here:
How do I merge two dictionaries in a single expression in Python?
(43 answers)
Closed 4 years ago.
Hello, everyone.
Is there a way in python to insert all items from one dictionary to another inside variable assignment?
dict1 = {"a": 1, "b": 2, "c": 3}
dict2 = {"d": 4, #INSERT THERE ALL FROM "dict1"#, "e": -1}
Maybe there's smth like {key: value for key, value in temp.items()} or other "hack"?
I know that there's update() method and I've already applied it, but it looks a bit weird. Order of entries matters so to build dict in proper order I need to write next code :
dict1 = {"a": 1, "b": 2, "c": 3}
dict2 = {"d: 4"}
dict2.update(dict1)
dict2.update({"e": -1, "f": -2})
Hope there's a way to do it more "nice".
dict2 = {"d": 4, **dict1, "e": -1}

Python: How to get the updated dictionary in 1 line? [duplicate]

This question already has answers here:
How do I merge two dictionaries in a single expression in Python?
(43 answers)
Closed 6 years ago.
In python, I have a dictionary named dict_a :
dict_a = {'a':1}
and I want to get a new dictionary dict_b as the update of dict_a,at the same time I don't want to change the dict_a, so I used this:
dict_b = copy.deepcopy(dict_a).update({'b':2})
However, the return value of dict.update is None, so the above code doesn't work. And I have to use this:
temp = copy.deepcopy(dict_a)
temp.update({'b':2})
dict_b = temp
The question is, how to get my goal in one line? Or some
What about:
>>> a = {'a':1}
>>> update = {'b': 1}
>>> b = dict(a.items() + update.items())
>>> b
{'a': 1, 'b': 1}
update - is your value that need to update(extend) dict a
b - is a new resulting dict
Anfd in this case a stay unchanged

How to interchange keys and values in a dict? [duplicate]

This question already has answers here:
How do I exchange keys with values in a dictionary? [duplicate]
(19 answers)
Closed 7 years ago.
I want to interchange keys and value in a given dictionary. For example:
dict = {1:'sandeep', 2: 'suresh', 3: 'pankaj'}
will become:
dict1 = {'sandeep': 1, 'suresh': 2, 'pankaj': 3}
Using zip is probably better than a dictionary comprehension:
dict1 = dict(zip(dict1.values(), dict1.keys())) # better for beginners
In [45]: d = {1:'sandeep',2 : 'suresh', 3: 'pankaj'}
In [46]: {(v, k) for k,v in d.iteritems()}
Out[46]: {'pankaj': 3, 'sandeep': 1, 'suresh': 2}
Note the use of d as the variable name instead of the built-in dict.
Also note that you are not guaranteed uniqueness and can loose entries:
In [47]: d = {1:'sandeep',2 : 'suresh', 3: 'pankaj', 4:'suresh'}
In [48]: {(v, k) for k,v in d.iteritems()}
Out[48]: {'pankaj': 3, 'sandeep': 1, 'suresh': 4}

Categories

Resources