Last active
March 18, 2016 02:45
-
-
Save for-coursera/827c1f94aac1cc01e084 to your computer and use it in GitHub Desktop.
List Less Than Ten, 1-2: http://www.practicepython.org/exercise/2014/02/15/03-list-less-than-ten.html
This file contains hidden or 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
| # === | |
| # 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]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why the
list(compress())?