List Comprehension Flashcards

1
Q

what’s list comprehension?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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)

A

fruits = [“apple”, “banana”, “cherry”, “kiwi”, “mango”]

newlist = [x for x in fruits if “a” in x]

print(newlist)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

whats the syntax of LC?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Only accept items from fruits that are not “apple”:

A

newlist = [x for x in fruits if x != “apple”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Accept only numbers lower than 5:

A

newlist = [x for x in range(10) if x < 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Set the values in the new list to upper case:

A

newlist = [x.upper() for x in fruits]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Set all values in the new list in fruits to ‘hello’:

A

newlist = [‘hello’ for x in fruits]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Return “orange” instead of “banana” from fruits

A

newlist = [x if x != “banana” else “orange” for x in fruits]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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?

A

The list comprehension is [doc[0] for doc in doctor] and produces the list [‘h’, ‘c’, ‘c’, ‘t’, ‘w’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

–# Create list comprehension: squares

squares = [i**2 for i in range(0,10)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

to create a list of lists, you simply have to supply the list comprehension as the output expression of the overall list comprehension

A

[[output expression] for iterator variable in iterable]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A

– 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

the following list comprehension explained:

matrix = [[col for col in range(5)] for row in range(5)]

for row in matrix:
print(row)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Use member as the iterator variable in the list comprehension. For the conditional, use len() to evaluate the iterator variable. Note that you only want strings with 7 characters or more.

fellowship = [‘frodo’, ‘samwise’, ‘merry’, ‘aragorn’, ‘legolas’, ‘boromir’, ‘gimli’]

A

–Create a list of strings: fellowship
fellowship = [‘frodo’, ‘samwise’, ‘merry’, ‘aragorn’, ‘legolas’, ‘boromir’, ‘gimli’]

new_fellowship = [member for member in fellowship if len(member) >= 7]

–Print the new list
print(new_fellowship)

17
Q
A