Member-only story
7 Essential Python Features You Should Be Using More Often
2 min readOct 5, 2024
1. List Comprehensions
List comprehensions offer a concise way to create lists. They are faster and more readable than traditional loops.
In [3]:
# Traditional way
squares = []
for x in range(10):
squares.append(x**2)
# List comprehension
squares = [x**2 for x in range(10)]
2. Enumerate
Instead of manually tracking indexes when looping, enumerate allows you to access both the index and value of an iterable in a clean way.
names = ['Alice', 'Bob', 'Charlie']
for index, name in enumerate(names, start=1):
print(index, name)
1 Alice
2 Bob
3 Charlie
3. Unpacking
Python’s multiple assignment and unpacking features make variable assignments cleaner, especially with tuples and lists.
a, b = 5, 10
first, *middle, last = [1, 2, 3, 4, 5]