Python 101: The Power of Lists

Understanding list comprehensions in Python.

Why Write 5 Lines When 1 Will Do?

In C++ or Java, creating a list of squares requires a loop. In Python, we use List Comprehensions.

The Old Way

squares = []
for i in range(10):
    squares.append(i * i)
print(squares)

The Pythonic Way

Notice how much cleaner this is:

Look at this beauty

squares = [i * i for i in range(10)]
print(squares)