Overview
Stop juggling index variables. enumerate counts for you and zip walks several lists side by side.
- enumerate(items, start=1)
- zip() pairs items and stops at the shortest list
- Unpacking in the for statement
Short Course
enumerate gives you a counter with each item; zip pairs items from
several lists and stops at the shortest one.
fruits = ["apple", "kiwi"]
prices = [1.2, 0.5]
for i, fruit in enumerate(fruits, start=1):
print(i, fruit) # 1 apple, 2 kiwi
for fruit, price in zip(fruits, prices):
print(f"{fruit}: {price}") # apple: 1.2 ...
Task
Write leaderboard(names, scores) that returns a list of strings like
"1. Ana (90)", numbered from 1 in the given order. If the lists have
different lengths, stop at the shorter one.
Test Suites
Pending
Stdout