These three built-in Python tools are essential for looping, tracking and organizing data efficiently without writing bulky manual loops.
Function | What it returns / does | Best Used For |
|---|---|---|
| It pairs the 1st items of two lists together, then the 2nd items, then the 3rd items, and stops when the shortest list runs out. | Looping over two or more lists at the exact same time. |
| The | Keeping track of the loop counter / index automatically. |
| The | Sorting complex data (like dicts or tuples) by specific fields. |
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
# Loop through both lists simultaneously
for name, score in zip(names, scores):
print(f"{name} scored {score}")
# Alice scored 85
# Bob scored 92
# Charlie scored 78tasks = ["Write code", "Test app", "Deploy to production"]
# Get the counter and the item at the same time
for index, task in enumerate(tasks, start=1): # Can change start from default 0 to 1
print(f"Task {index}: {task}")
# Task 1: Write code
# Task 2: Test app
# Task 3: Deploy to production
fruits = ["banana", "strawberry", "apple"]
# Sort items based on the result of the len() function
ordered_fruits = sorted(fruits, key=len)
print(ordered_fruits)
# Output: ['apple', 'banana', 'strawberry']