Skip to content Skip to sidebar Skip to footer

How To Construct Nested Dictionary Comprehension In Python With Correct Ordering?

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. H

Solution 1:

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"])

Post a Comment for "How To Construct Nested Dictionary Comprehension In Python With Correct Ordering?"