Python Forum

Full Version: How to make this simpler.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
D = ['a','b','c','d','e']

l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]


ND = {}

for x in l:
    if x[1] in D:
        ND[x[1]] = []
for x in l:
    ND[x[1]].append(x[0])


print(ND)
I'm trying to make a dictionary based on the two lists above. "D" is the list for keys. "l" is a "data pool". I got what I want, but I'm still looking for a better solution.
One thing you can do is just delete line 11. Leave what is now line 12 indented and it will still work.

Another solution is collections.defaultdict:

import collections

l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]
ND = collections.defaultdict(list)

for x in l:
    ND[x[1]].append(x[0])
(Feb-26-2019, 04:45 PM)ichabod801 Wrote: [ -> ]One thing you can do is just delete line 11. Leave what is now line 12 indented and it will still work.

Another solution is collections.defaultdict:

import collections

l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]
ND = collections.defaultdict(list)

for x in l:
    ND[x[1]].append(x[0])

Thank you for the solution. But I'm using IronPython, trying to import as less as I can.

D = ['a', 'b', 'c', 'd', 'e']
l = [[233, 'a'], [321, 'a'], [445, 'b'], [322, 'c'], [123, 'c'], [12312, 'c'], [456, 'd'], [334, 'e']]

ND = dict.fromkeys(D,[])

print (ND)


for x in l:
    ND[x[1]].append(x[0])

print(ND)
I tried using dict.fromkeys, but it give me a different result. It will put all x[0] under each key of the dictionary. Comparing with the script below, their first "print" give same kind of dictionary. Their second steps are the same. Could anyone tell me why the results are different?

D = ['a', 'b', 'c', 'd', 'e']
l = [[233, 'a'], [321, 'a'], [445, 'b'], [322, 'c'], [123, 'c'], [12312, 'c'], [456, 'd'], [334, 'e']]

ND = {}

for x in l:
    if x[1] in D:
        ND[x[1]] = []

print (ND)

for x in l:
    ND[x[1]].append(x[0])

print(ND)
Dictionaries and lists are mutable objects, so when you reassign them, you reassign a link (or pointer) to them, to the value they hold. So your dict.fromkeys is assigning the same empty list to each dictionary key. To make a dictionary of independent empty lists you can use a dictionary comprehension:

ND = {key: [] for key in D}
(Feb-26-2019, 05:52 PM)leoahum Wrote: [ -> ]I'm using IronPython, trying to import as less as I can.

I looks like Iron Python does have the collections library, which is part of the standard library for C Python: https://ironpython-test.readthedocs.io/e...tions.html
(Feb-26-2019, 06:35 PM)ichabod801 Wrote: [ -> ]
(Feb-26-2019, 05:52 PM)leoahum Wrote: [ -> ]I'm using IronPython, trying to import as less as I can.

I looks like Iron Python does have the collections library, which is part of the standard library for C Python: https://ironpython-test.readthedocs.io/e...tions.html

Thanks a lot for your help!
One way to achieve desired result without import:

>>> D = ['a','b','c','d','e']
>>> l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]
>>> d = dict()
>>> for v, k in l:
...     if k in D:
...         d.setdefault(k, []).append(v)
...
>>> d
{'a': [233, 321], 'b': [445], 'c': [322, 123, 12312], 'd': [456], 'e': [334]}
(Mar-06-2019, 11:23 AM)perfringo Wrote: [ -> ]One way to achieve desired result without import:

>>> D = ['a','b','c','d','e']
>>> l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]
>>> d = dict()
>>> for v, k in l:
...     if k in D:
...         d.setdefault(k, []).append(v)
...
>>> d
{'a': [233, 321], 'b': [445], 'c': [322, 123, 12312], 'd': [456], 'e': [334]}

Thanks!