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}
Related
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}
This question already has answers here:
Is it possible to assign the same value to multiple keys in a dict object at once?
(8 answers)
Closed 3 years ago.
Is there a compact way to have alternative keys for the same value in a dictionary?
Taking a dictionary like the following
dict={'A':1,'B':2,'one':1,'two':2}
I can obtain the value 1 using two different keys:
dict['A']
dict['one']
I would like to know if there is a more compact way to write it, something akin to:
dict2={['A','one']:1,['B','two']:2}
You can define the dict with keys of the same value grouped as a tuple first:
d = {('A', 'one'): 1, ('B', 'two'): 2}
so that you can then convert it to the desired dict with:
d = {key: value for keys, value in d.items() for key in keys}
d becomes:
{'A': 1, 'one': 1, 'B': 2, 'two': 2}
This question already has answers here:
How do I merge a list of dicts into a single dict?
(11 answers)
Closed 5 years ago.
Right now i have this:
A={0:[{1:2},{2:3}],
1:[{4:5},{5:6}]}
And I want to change this dictionary to the following:
A={0:{1:2,2:3},
1:{4:5,5:6}}
Is this possible?
Try doing this: Iterate over the elements int the list and merge them into temp_dict. assign temp_dict to the key of the original dictionary.
A={0:[{1:2},{2:3}],
1:[{4:5},{5:6}]}
for k,v in A.items():
temp_dict={}
for elem in v:
for k2,v2 in elem.items():
temp_dict[k2] = v2
A[k] = temp_dict
print A
# prints {0: {1: 2, 2: 3}, 1: {4: 5, 5: 6}}
This question already has answers here:
switching keys and values in a dictionary in python [duplicate]
(10 answers)
Closed 6 years ago.
I am currently preparing for a python exam and one topic we are expected to understand is having to flip a dictionary in which values become the keys and the keys become values. I am confused as to what this asking and if someone could provide me with a basic example to see what it looks like I would greatly appreciate it.
Simply write a dict comprehension expression and make it's key as value and values as key. For example:
>>> my_dict = {1: 2, 3: 4, 5: 6}
>>> {value: key for key, value in my_dict.items()}
{2: 1, 4: 3, 6: 5}
Note: Since, dict contains unique keys. In case you have same element as value for multiple keys in your original dict, you will loose related entries. For example:
# Same values v v
>>> my_dict = {1: 2, 3: 4, 5: 2}
>>> {value: key for key, value in my_dict.items()}
{2: 5, 4: 3}
#^ Only one entry of `2` as key
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.