Skip to content

Instantly share code, notes, and snippets.

@for-coursera
Last active March 18, 2016 02:45
Show Gist options
  • Select an option

  • Save for-coursera/827c1f94aac1cc01e084 to your computer and use it in GitHub Desktop.

Select an option

Save for-coursera/827c1f94aac1cc01e084 to your computer and use it in GitHub Desktop.
# ===
# Given a list, write a program that prints out all the elements of the list that are less than 5
# * Make a new list that has all the elements less than 5 from this list in it and print out this new list
# * Write this in one line of Python
# ===
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
# take 1
print([e for e in a if e < 5])
# take 2
from itertools import compress
print(list(compress(a, [e < 5 for e in a])))
@mprat

mprat commented Mar 18, 2016

Copy link
Copy Markdown

Why the list(compress())?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment