Beginner·6 min·basics · iteration
enumerate, zip, range
enumerate, zip, range
Three iteration patterns you'll reach for every day.
enumerate(seq, start=0)
Gives you (index, value) tuples. Stop writing for i in range(len(x)).
zip(a, b)
Walks multiple sequences in parallel. Stops at the shortest one.
range(start, stop, step)
Lazy sequence of integers. Convert with list(range(...)) if you need a list.
Try it
- Use
zipto build a dict fromnamesandyears. - Print every other item of a list using
enumerate+ a check on the index.