Nearby lessons

48 of 159

Python - zip() Function

📌 What You Will Learn
  • Explain how zip() combines iterables
  • Unpack zipped values in a for loop
  • Use zip() to create dictionaries
  • Handle iterables with unequal lengths
  • Convert a zip object into a list

Combining related sequences

zip() is useful when values at the same position belong together, such as names and scores.

Basic syntax

Use zip(iterable1, iterable2, ...). Each result item is a tuple containing one value from each input.

Loop over two lists together

Loading editor…

Inspecting the zip object

Loading editor…

Building a dictionary

Loading editor…

Unequal lengths

Regular zip() stops at the shortest iterable, so extra values in longer inputs are ignored.

Loading editor…

Unzipping pairs

Use the unpacking operator with zip() to separate pairs back into columns.

Loading editor…

When to use zip_longest()

Import zip_longest() from itertools when every item from unequal-length inputs should be processed with a fill value.

📝 Key Takeaways
  • zip() groups corresponding elements into tuples
  • The result is a lazy zip iterator
  • Iteration stops when the shortest iterable is exhausted
  • dict(zip(keys, values)) is a convenient dictionary pattern
  • Use itertools.zip_longest() when missing values should be retained

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.