Category: python
Combining Iterables with zip()
Published on 27 Oct 2026
Explanation
zip() combines corresponding elements and stops at the shortest input iterable by default.
Code:
names = ['Arun', 'Priya'] marks = [85, 92] for name, mark in zip(names, marks): print(name, mark)
Explanation
Python 3.10+ supports strict=True to raise ValueError when zip inputs have different lengths.
Code:
names = ['Arun', 'Priya', 'Kumar'] marks = [85, 92] print(list(zip(names, marks))) # list(zip(names, marks, strict=True)) raises ValueError
Explanation
enumerate() yields an index and item, avoiding a manual counter in a loop.
Code:
languages = ['Python', 'Java', 'JavaScript'] for index, language in enumerate(languages): print(index, language)
Explanation
The start argument controls the first index, often 1 for numbered lists shown to users.
Code:
topics = ['Variables', 'Functions', 'Classes'] for number, topic in enumerate(topics, start=1): print(number, topic)
Explanation
Combine these functions to process related collections while also tracking row numbers.
Code:
names = ['Arun', 'Priya'] marks = [85, 92] for row, (name, mark) in enumerate(zip(names, marks), start=1): print(row, name, mark)