Created
February 7, 2013 21:24
-
-
Save lambdamusic/4734281 to your computer and use it in GitHub Desktop.
Python: 'Dictionary Comprehensions'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
emails = {'Dick': 'bob@example.com', 'Jane': 'jane@example.com', 'Stou': 'stou@example.net'} | |
email_at_dotcom = dict( [name, '.com' in email] for name, email in emails.iteritems() ) | |
# email_at_dotcom now is {'Dick': True, 'Jane': True, 'Stou': False} | |
# ANOTHER OPTION TO CREATE DICTS ON THE FLY: | |
d = dict([("n= %s" % x, x) for x in range(10)]) | |
# {'n= 9': 9, 'n= 8': 8, 'n= 3': 3, 'n= 2': 2, 'n= 1': 1, 'n= 0': 0, 'n= 7': 7, 'n= 6': 6, 'n= 5': 5, 'n= 4': 4} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment