Python Forum
Dictionary from a list failed, help needed - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Dictionary from a list failed, help needed (/thread-37046.html)



Dictionary from a list failed, help needed - leoahum - Apr-26-2022

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



RE: Dictionary from a list failed, help needed - bowlofred - Apr-26-2022

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]}



RE: Dictionary from a list failed, help needed - leoahum - Apr-26-2022

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]}



RE: Dictionary from a list failed, help needed - Pedroski55 - Apr-27-2022

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!


RE: Dictionary from a list failed, help needed - deanhystad - Apr-27-2022

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]}



RE: Dictionary from a list failed, help needed - Pedroski55 - Apr-27-2022

Very cool! Well done!

I would never have figured thatt out!


RE: Dictionary from a list failed, help needed - leoahum - Apr-27-2022

Thank you all for the wonderful solutions!


RE: Dictionary from a list failed, help needed - buran - Apr-28-2022

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)