Skip to content Skip to sidebar Skip to footer

Python: Dict Comprehensions: Making A New Dictionary With New Values From Old Dictionary Ids

I have a simple dictionary. I am trying to create a new dictionary with the ids from the old dictionary and the lengths of the values from the old dictionary as values for the new

Solution 1:

You're mixing the dictionary comprehension and the for loop, which both do more or less the same thing. Just do this:

new_dict = { k: len(v) for k, v in old_dict.iteritems()}

Post a Comment for "Python: Dict Comprehensions: Making A New Dictionary With New Values From Old Dictionary Ids"