Skip to content

Instantly share code, notes, and snippets.

@carlwgeorge
Created January 4, 2018 19:17
Show Gist options
  • Save carlwgeorge/d72bdc4867eb4c3a17093d24d796e867 to your computer and use it in GitHub Desktop.
Save carlwgeorge/d72bdc4867eb4c3a17093d24d796e867 to your computer and use it in GitHub Desktop.

dataset:

names = ['John', 'Mary', 'Sue', 'Jill', 'Garth', 'Jack', 'Jennifer']

before:

names_that_start_with_j = []
for name in names:
    if name.startswith('J'):
        names_that_start_with_j.append(name)

after:

names_that_start_with_j = [name for name in names if name.startswith('J')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment