Python Forum

Full Version: Dictionary from a list failed, help needed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am trying to get a dictionary from a list like ["a", "b", "c", "a", "c"]. The out come is expected to be {"a": [0, 3], "b": [1], "c": [2, 4]}. The element of the list is the key and index of the element is the value. My script below doesn't work. Need some help with why it doesn't work and a solution. Thanks!

l = ["a","b","c","a","c"]

#create a dictionary with l's elements
d ={}.fromkeys(l, [])


#assign key and value by for loop
enum = enumerate(l)
for i,j in enum:
    d[j].append(i)
    
print d
Line4 is a problem. It assumes there is a single default value and assigns that to each. In your case that means each key is assigned the same empty list. When any key is updated, all the keys get updated because they're sharing the same list.

There are several ways to do this, but I might use setdefault(). I would append the position to the output of setdefault(). It will be the list if it already exists, or an empty list if it doesn't.

Your print line doesn't have parentheses. Are you running this under python2 or python3?

l = ["a","b","c","a","c"]

#create a dictionary
d = {}

#assign key and value by for loop
enum = enumerate(l)
for i,j in enum:
    d.setdefault(j, []).append(i)

print (d)
Output:
{'a': [0, 3], 'b': [1], 'c': [2, 4]}
Thanks a lot. It is super helpful! FYI, I'm using Ironpython 2.7 that embeded in some other software.

(Apr-26-2022, 08:58 PM)bowlofred Wrote: [ -> ]Line4 is a problem. It assumes there is a single default value and assigns that to each. In your case that means each key is assigned the same empty list. When any key is updated, all the keys get updated because they're sharing the same list.

There are several ways to do this, but I might use setdefault(). I would append the position to the output of setdefault(). It will be the list if it already exists, or an empty list if it doesn't.

Your print line doesn't have parentheses. Are you running this under python2 or python3?

l = ["a","b","c","a","c"]

#create a dictionary
d = {}

#assign key and value by for loop
enum = enumerate(l)
for i,j in enum:
    d.setdefault(j, []).append(i)

print (d)
Output:
{'a': [0, 3], 'b': [1], 'c': [2, 4]}
I don't know anything about dict.setdefault() or where it might be useful.

You could achieve the same result like this:

mylist = ["a", "b", "c", "a", "c"]
# make a dictionary of empty lists
mydict = {l:[] for l in mylist}
# populate the lists
for v, k in enumerate(mylist):
    mydict[k].append(v)
How to put these 2 steps in a one-liner, I don't know!
This is not easy to read:
l = ["a","b","c","a","c"]
d = {key:[index for index, value in enumerate(l) if value == key] for key in l}
print (d)
Output:
{'a': [0, 3], 'b': [1], 'c': [2, 4]}
Very cool! Well done!

I would never have figured thatt out!
Thank you all for the wonderful solutions!
Just for sake of completeness, to mention collections.defaultdict

from collections import defaultdict
mylist = ["a", "b", "c", "a", "c"]
result = defaultdict(list)
for idx, item in enumerate(mylist):
    result[item].append(idx)
print(result)