
(Tutorial) List comprehensions in Python with examples and code.
What is a list comprehension?
Simply put, a list comprehension is a pythonic shortcut of a ‘for-loop’. For example, when we want to raise every number to the power of two we can do a simple for loop:
range_100_items = range(100)
squares = []
for x in range_100_items:
squares.append(x**2)