Merge pull request #68 from lucis-fluxum/python-list-comprehension

Add python list comprehension sheet
pull/73/head
Igor Chubin 5 years ago committed by GitHub
commit 3f52721f6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,12 @@
# 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]
Loading…
Cancel
Save