Skip to content

Instantly share code, notes, and snippets.

@samdmarshall
Created August 26, 2016 22:57
Show Gist options
  • Save samdmarshall/07405cfc01b04f2a5bd04ee57575aa7d to your computer and use it in GitHub Desktop.
Save samdmarshall/07405cfc01b04f2a5bd04ee57575aa7d to your computer and use it in GitHub Desktop.
# 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