mirror of
https://github.com/chubin/cheat.sheets
synced 2024-11-05 12:00:16 +00:00
13 lines
482 B
Plaintext
13 lines
482 B
Plaintext
# List comprehensions are a compact way to define a list
|
|
squares = [x**2 for x in range(1, 10)]
|
|
# squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
|
|
|
|
# You can also add a condition to filter out some of the elements
|
|
evens = [i for i in range(1, 10) if i % 2 == 0]
|
|
# evens = [2, 4, 6, 8]
|
|
|
|
# If your list is multi-dimensional, you can nest for-loops
|
|
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
|
flattened = [val for row in matrix for val in row]
|
|
# flattened = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|