Created
August 26, 2016 22:57
-
-
Save samdmarshall/07405cfc01b04f2a5bd04ee57575aa7d to your computer and use it in GitHub Desktop.
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
# so instead of doing this: | |
my_list = list() | |
for key in dictionary: | |
if dictionary[key] == 'foo': | |
my_list.append(key) | |
# you can use list comprehension to write code like this: | |
my_list = [key for key in dictionary if dictionary[key] == 'foo'] | |
# both produce the same list of keys, but i think that using list comprehension is a lot more readable. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment