Hackforge Academy

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)

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community