List Comprehension
List Comprehension Example 1
# Old Method
squares = []
for i in range(10):
squares.append(i ** 2)
print('Our result: %s' % squares)Our result: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]# List Comprehension
squares = [i**2 for i in range(10)]
print('Our new result: %s' % squares)How Does it Work?
List Comprehension Example 2
List Comprehension Example 3
Last updated