Skip to content

Instantly share code, notes, and snippets.

@azdafirmansyah
Last active November 30, 2017 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azdafirmansyah/c025f233dfd6079926fbe572222f429c to your computer and use it in GitHub Desktop.
Save azdafirmansyah/c025f233dfd6079926fbe572222f429c to your computer and use it in GitHub Desktop.
List Comprehension
#Exercise URL : http://www.practicepython.org/exercise/2014/03/19/07-list-comprehensions.html
'''
Let’s say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100].
Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.
'''
list_data = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
list_even = [x for x in list_data if x%2 == 0]
list_odd = [x for x in list_data if x%2 != 0]
print(list_even)
print(list_odd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment