Code Fellows Python 401
- In Python, List Comprehensions are a more compact way to create and manage lists
- faster and more flexible than for loops
my_new_list = [ expression for item in list ]three main parts(in square brackets):
expression- expression we’d like to carry outitem- object that the expression will work onlist- iterable list of objects to build our new list from
# construct a basic list using range() and list comprehensions
# syntax
# [ expression for item in list ]
digits = [x for x in range(10)]
print(digits)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]- the first
xdoesn't do anything(just records a number) - second
xis the item created
# create a list using a for loop
squares = []
for x in range(10):
# raise x to the power of 2
squares.append(x**2)
print(squares)# create a list using list comprehension
squares = [x**2 for x in range(10)]
print(squares)[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]- List comprehension reduces the amount of code needed to produce the same output
# create a list with list comprehensions
multiples_of_three = [ x*3 for x in range(10) ]
print(multiples_of_three)[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]# create a list with list even numbers
even_numbers = [ x for x in range(1,20) if x % 2 == 0]# a list of the names of popular authors
authors = ["Ernest Hemingway","Langston Hughes","Frank Herbert","Toni Morrison",
"Emily Dickson","Stephen King"]
# create an acronym from the first letter of the author's names
letters = [ name[0] for name in authors ]
print(letters)['E', 'L', 'F', 'T', 'E', 'S']# use list comprehension to print the letters in a string
letters = [ letter for letter in "20,000 Leagues Under The Sea"]
print(letters)['2', '0', ',', '0', '0', '0', ' ', 'L', 'e', 'a', 'g', 'u', 'e', 's', ' ', 'U', 'n', 'd', 'e', 'r', ' ', 'T', 'h', 'e', ' ', 'S', 'e', 'a']lower_case = [ letter.lower() for letter in ['A','B','C'] ]
upper_case = [ letter.upper() for letter in ['a','b','c'] ]
print(lower_case, upper_case)['a', 'b', 'c'] ['A', 'B', 'C']# user data entered as name and phone number
user_data = "Elvis Presley 987-654-3210"
phone_number = [ x for x in user_data if x.isdigit()]
print(phone_number)['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']- you can even read files usung list comprehension
Hold fast to dreams
For if dreams die
Life is a broken-winged bird
That cannot fly.
-Langston Hughes# open the file in read-only mode
file = open("dreams.txt", 'r')
poem = [ line for line in file ]
for line in poem:
print(line)Hold fast to dreams
For if dreams die
Life is a broken-winged bird
That cannot fly.
-Langston Hughs# list comprehension with functions
# create a function that returns a number doubled
def double(x):
return x*2
nums = [double(x) for x in range(1,10)]
print(nums)[2, 4, 6, 8, 10, 12, 14, 16, 18]# add a filter so we only double even numbers
even_nums = [double(x) for x in range(1,10) if x%2 == 0]
print(even_nums)[4, 8, 12, 16]nums = [x+y for x in [1,2,3] for y in [10,20,30]]
print(nums)[11, 21, 31, 12, 22, 32, 13, 23, 33]