简体   繁体   中英

Python: Create a dictionary where keys have multiple values

The problem that I have is hard to explain, easy to understand:

I have a list of tuples:

L=[('a','111'),('b','222'),('a','333'),('b','444')]

from this list I want to createa dictionary where the keys are the first elements of the tuples ('a' and 'b') and the values associated are in a list:

expected output:

{'a':['111','333'],'b':['222','444']}

How can I solve this problem?

d={}
for x in range (len(L)):
    d[L[x][0]]=[L[x][1]]
return d

but as you can easy understand, the output won't be complete since the list will show just the last value associated to that key in L

You can usesetdefault() to set the key in the dict the first time. Then append your value:

L=[('a','111'),('b','222'),('a','333'),('b','444')]

d = {}
for key, value in L:
    d.setdefault(key, []).append(value)

print(d)
# {'a': ['111', '333'], 'b': ['222', '444']}

You have to append L[x][1] to an existing list, not replace whatever was there with a new singleton list.

d={}
for x in range (len(L)):
    if L[x][0] not in d:
        d[L[x][0]] = []
    d[L[x][0]].append(L[x][1])
return d

A defaultdict makes this easier:

from collections import defaultdict

d = defaultdict(list)
for x in range(len(L)):
    d[L[x][0]].append(L[x][1])
return d

A more idiomatic style of writing this would be to iterate directly over the list and unpack the key and value immediately:

d = defaultdict(list)
for key, value in L:
    d[key].append(value)

You can try this:

L = [('a','111'),('b','222'),('a','333'),('b','444')]
my_dict = {}

for item in L:
    if item[0] not in my_dict:
        my_dict[item[0]] = []

    my_dict[item[0]].append(item[1])

print(my_dict)

Output:

python your_script.py
{'a': ['111', '333'], 'b': ['222', '444']}

As pointed by @chepner, you can use defaultdict to.

Basically, with defaultdict you'll not need to check if there is no key yet in your dict .

So it would be:

L = [('a','111'),('b','222'),('a','333'),('b','444')]
my_dict = defaultdict(list)

for item in L:
    my_dict[item[0]].append(item[1])

print(my_dict)

And the output:

defaultdict(<class 'list'>, {'a': ['111', '333'], 'b': ['222', '444']})

And if you want to get a dict from the defaultdict , you can simply create a new dict from it:

print(dict(my_dict))

And the output will be:

{'a': ['111', '333'], 'b': ['222', '444']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM