Phase 5 — Python-specific Features
enumerate
Loop over a list while also getting the index
enumerate() wraps an iterable so each loop turn gives you both the index and the
value.
users = ["John", "David"]
for index, user in enumerate(users):
print(index, user)0 John
1 DavidJS has a couple of ways to get the same result:
const users = ['John', 'David'];
users.forEach((user, index) => {
console.log(index, user);
});
// or, if you need a real `for` loop:
for (const [index, user] of users.entries()) {
console.log(index, user);
}arr.entries() is the closest match — it also gives you [index, value] pairs, same shape
as Python's enumerate().
Starting from a different number
Pass start if you don't want to begin counting at 0:
for index, user in enumerate(users, start=1):
print(index, user)1 John
2 David