'Dictionary Comprehensions'


/ Published in: Python
Save to your folder(s)

Although Python doesn't have built-in dictionary comprehensions, you can do something pretty close with little mess or code. Just use .iteritems() to turn your dict into a list, throw it in a generator expression (or list comprehension), and then cast that list back into a dict.

For example, say I have a dictionary of name:email pairs, and I want to create a dictionary of name:is_email_at_a_dot_com pairs:

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.