How to construct nested dictionary comprehension in Python with correct ordering?

6.6k Views Asked by At

I was trying to shorten the code for this problem when I encountered the problem.

Basically, I was trying a nested dictionary comprehension & was unsuccessful in the attempt. Here is what I tried.

dict2 = {key:value for key, value in line.split(":")
                   for line in ["1:One", "2:Two", "4:Four"]}
print dict2

When I run this, it gives me

NameError: name 'line' is not defined

And, when I reverse the for statements like this

dict2 = {key:value for line in ["1:One", "2:Two", "4:Four"]
                   for key, value in line.split(":")}
print dict2

It results in

ValueError: need more than 1 value to unpack

I need help on the nesting structure of dictionary (or list) comprehension. An example would be help a lot.

2

There are 2 best solutions below

12
On BEST ANSWER

Note that there is a better way of doing this without a dict comprehension; see below. I’ll first address the issues with your approach.

You need to use nesting order in comprehensions. List your loops in the same order they would be in when nesting a regular loop.

The line.split() expression returns a sequence of two items, but each of those items is not a tuple of a key and a value; instead there is only ever one element being iterated over. Wrap the split in a tuple so you have a 'sequence' of (key, value) items being yielded to assign the two results to two items:

dict2 = {key:value for line in ["1:One", "2:Two", "4:Four"]
                   for key, value in (line.split(":"),)}

This is the equivalent of:

dict2 = {}
for line in ["1:One", "2:Two", "4:Four"]:
    for key, value in (line.split(":"),):
        dict2[key] = value

where the nested loop is only needed because you cannot do:

dict2 = {}
for line in ["1:One", "2:Two", "4:Four"]:
    key, value = line.split(":")
    dict2[key] = value

However, in this case, instead of a dictionary comprehension, you should use the dict() constructor. It wants two-element sequences, simplifying the whole operation:

dict2 = dict(line.split(":") for line in ["1:One", "2:Two", "4:Four"])
3
On

You can do this a lot simpler with dict and a generator expression:

>>> lst = ["1:One", "2:Two", "4:Four"]
>>> dict(x.split(":") for x in lst)
{'4': 'Four', '1': 'One', '2': 'Two'}
>>>