Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List Comprehension Syntax
# Creating a List of numbers from 1 to 10 can be done with:
list_1_to_10 = range(1,11)
# Returns
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# But what if you wanted only the even numbers? Use the List comprehension syntax!
even_num_list = [x for x in range(1,11) if x % 2 == 0]
# Returns
[2, 4, 6, 8, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment