21
loading...
This website collects cookies to deliver better user experience
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
range
to create a generator, then append
to that array using a for
loopnumbers = []
for x in range(3):
numbers.append(x)
numbers = [x for x in range(3)]
numbers = [x*2 for x in range(3)]
for
loop. This comprises of three separate parts:range
. This acts as the iterator for the for
loop to iterate through. This can be replaced with anything a for
loop can go through: a list, a tuple, or anything else that implements the iterator interface.map
, we can pass an anonymous function (lambda) to multiply a number by 2, pass the range
to iterate through. However, once this is done, we’re left with a map
object. In order to convert this back to a list, we have to wrap that method in list
.numbers = list(map(lambda x: x*2, range(3)))
numbers = [x*2 for x in range(3)]
map
.map
, you’re actually able to implement logic more similar to filter
to change how many items are in the output compared to what was input.if
to the end of the statement, we can limit the output to only even numbers:even_numbers = [x for x in range(10) if x%2==0] #[0, 2, 4, 6, 8]
double_even_numbers = [x*2 for x in range(10) if x%2==0] #[0, 4, 8, 12, 16]
if
in a list comprehension, you’re able to use them to act as conditionals to return different values from the original.number_even_odd = ["Even" if x % 2 == 0 else "Odd" for x in range(4)]
# ["Even", "Odd", "Even", "Odd"]
if
:thirds_even_odd = ["Even" if x % 2 == 0 else "Odd" for x in range(10) if x%3==0]
# [0, 3, 6, 9] after filtering numbers
# ["Even", "Odd", "Even", "Odd"] after ternary to string
thirds_even_odd = []
for x in range(10):
if x%3==0:
if x%2==0:
thirds_even_odd.append("Even")
else:
thirds_even_odd.append("Odd")
repeated_list = [y for x in ["", ""] for y in [1, 2, 3]]
# [1, 2, 3, 1, 2, 3]
repeated_list = []
for x in ["", ""]:
for y in [1, 2, 3]:
repeated_list.append(y)
numbers_doubled = [y for x in [1, 2] for y in [x, x*2]]
# 1, 2, 2, 4
row_list = [[1, 2], [3,4], [5,6]]
indexed_list = []
for i in range(2):
indexed_row = []
for row in row_list:
indexed_row.append(row[i])
indexed_list.append(indexed_row)
print(indexed_list)
# [[1, 3, 5], [2, 4, 6]]
1
, 3
, 5
) are in the first array, and the second indexed items (2
, 4
, 6
) are in the second array.row_list = [[1, 2], [3,4], [5,6]]
indexed_list = [[row[i] for row in row_list] for i in range(2)]
print(indexed_list)
# [[1, 3, 5], [2, 4, 6]]
and
- Logical “and”or
- Logical “or”not
- Logical “not”is
- Equality checkin
- Membership check/second half of for
loopvowels = 'aeiou'
word = "Hello!"
word_vowels = [letter for letter in word if letter.lower() in vowels]
print(word_vowels)
# ['e', 'o']
word_consonants = [letter for letter in word if letter.lower() not in vowels]
# ['H', 'l', 'l']
restricted_number = 4
safe_numbers = [x for x in range(6) if (x%2==0 or x%3==0) and x is not restricted_number]
# [0, 2, 3]
for
loops.map
, filter
or other list helpers, either. Simply use nested for
loops and if
conditionals to match the behavior as it was before.