List Comprehension Flashcards
what’s list comprehension?
offers a shorter syntax when you want to create a new list based on the values of an existing list.
for ex, Based on a list of fruits, you want a new list, containing only the fruits with the letter “a” in the name.
Without list comprehension you will have to write a for statement with a conditional test inside
convert the following to list comprehension:
fruits = [“apple”, “banana”, “cherry”, “kiwi”, “mango”]
newlist = []
for x in fruits:
if “a” in x:
newlist.append(x)
print(newlist)
fruits = [“apple”, “banana”, “cherry”, “kiwi”, “mango”]
newlist = [x for x in fruits if “a” in x]
print(newlist)
whats the syntax of LC?
newlist = [expression for item in iterable if condition == True]
The condition is like a filter that only accepts the items that valuate to True.
The expression is the current item in the iteration, but it is also the outcome, which you can manipulate before it ends up like a list item in the new list:
newlist = [x.upper() for x in fruits]
Only accept items from fruits that are not “apple”:
newlist = [x for x in fruits if x != “apple”]
Accept only numbers lower than 5:
newlist = [x for x in range(10) if x < 5]
Set the values in the new list to upper case:
newlist = [x.upper() for x in fruits]
Set all values in the new list in fruits to ‘hello’:
newlist = [‘hello’ for x in fruits]
Return “orange” instead of “banana” from fruits
newlist = [x if x != “banana” else “orange” for x in fruits]
doctor = [‘house’, ‘cuddy’, ‘chase’, ‘thirteen’, ‘wilson’]
How would a list comprehension that produces a list of the first character of each string in doctor look like? Note that the list comprehension uses doc as the iterator variable. What will the output be?
The list comprehension is [doc[0] for doc in doctor] and produces the list [‘h’, ‘c’, ‘c’, ‘t’, ‘w’]
Given the following objects below, which of these can we build list comprehensions over?
doctor = [‘house’, ‘cuddy’, ‘chase’, ‘thirteen’, ‘wilson’]
range(50)
underwood = ‘After all, we are nothing more or less than what we choose to reveal.’
jean = ‘24601’
flash = [‘jay garrick’, ‘barry allen’, ‘wally west’, ‘bart allen’]
valjean = 24601
type Next() and nest the name in the parenthesis. If it returns red nonsense, it’s not an iterable and therefore cant be list-comprehended
write a list comprehension that produces a list of the squares of the numbers ranging from 0 to 9.
Using the range of numbers from 0 to 9 as your iterable and i as your iterator variable, write a list comprehension that produces a list of numbers consisting of the squared values of i.
–# Create list comprehension: squares
squares = [i**2 for i in range(0,10)]
to create a list of lists, you simply have to supply the list comprehension as the output expression of the overall list comprehension
[[output expression] for iterator variable in iterable]
In the inner list comprehension - that is, the output expression of the nested list comprehension - create a list of values from 0 to 4 using range(). Use col as the iterator variable.
In the iterable part of your nested list comprehension, use range() to count 5 rows - that is, create a list of values from 0 to 4. Use row as the iterator variable; note that you won’t be needing this variable to create values in the list of lists.
– Create a 5 x 5 matrix using a list of lists: matrix
matrix = [[col for col in range(5)] for row in range(5)]
– Print the matrix
for row in matrix:
print(row)
the following list comprehension explained:
matrix = [[col for col in range(5)] for row in range(5)]
for row in matrix:
print(row)
The outer list comprehension is responsible for creating the rows of the matrix.
for row in range(5) iterates over a range of 5 values, which are 0, 1, 2, 3, 4. This means it will create 5 rows in total.
Although we use row as the iterator variable, it is not used in the output expression of the outer list comprehension. Its purpose is simply to repeat the inner list comprehension 5 times.
Inner List Comprehension:
The inner list comprehension is responsible for creating the columns of each row.
for col in range(5) iterates over a range of 5 values, which are 0, 1, 2, 3, 4. This means it will create 5 columns in each row.
col is used as the iterator variable in the output expression, so it generates a list of values [0, 1, 2, 3, 4] for each row.
Putting it all together:
The outer list comprehension runs 5 times (once for each row).
Each time the outer list comprehension runs, it executes the inner list comprehension, which generates a list [0, 1, 2, 3, 4].
As a result, the outer list comprehension collects these lists into a larger list, forming a 5x5 matrix.