List Comprehension
List comprehension provides a concise way
to create lists in Python.
Basic List Comprehension
myList.py
squares = [x**2 for x in range(5)]
print(squares)
Output
[0, 1, 4, 9, 16]
List Comprehension with Condition
myList.py
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares)
Output
[0, 4, 16, 36, 64]