List Comprehension Flashcards

1
Q

How would you create the list [0, 1, 2, 3, 4] using list comprehension in Python?

A

arr = [i for i in range(5)]

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

How would you double each value in a list using list comprehension in Python?

A

arr = [i + i for i in range(5)]

Result will be [0, 2, 4, 6, 8]

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