MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Setup
  • 1A: Fundamental Building Blocks
  • 1B: Compound Statements
  • 2: Ordered Collection
  • 3: Unordered Collection
  • 4: More Data types
  • 5: Iteration Constructs
    • Loops
    • Zip operation
    • Quiz
    • Colab Exercise
  • 6: Other constructs
  • 7. Regex
  • 8. Date and Time
  • Revision
  • Practice Exercise
  • Titanic Workshop

Zip

There are a few techniques that you may employ occasionally while manipulating your collections. Zip is one of them.

Zip is used to transform multiple collections into a single collection of tuples of respective elements.

list1 = ["a", "b", "c"]
list2 = [1, 2, 3, 4]
zipped = zip(list1, list2)
print(type(zipped))

for i in zipped:
    print(i)

Zip Operation

output:

< class 'zip' >
0 ('a', 1)
1 ('b', 2)
2 ('c', 3)

In the above example, you convert two lists of different data types into a zip object containing tuples with elements from each list in the order in which they were provided. If the lengths of the two lists are different, then the zip stops at the length of the shortest list.

A zip object can be enumerated as shown above or you can convert it into a list by using the list() function. However, once enumerated or converted to a list, the zip object will be empty and cannot be reused. See the example below:

list1 = ["a", "b", "c"]
list2 = [1, 2, 3, 4]
zipped = zip(list1, list2)
tuple_list = list(zipped)

for i in tuple_list:
    print(i)  # This will print the tuples.

for i, j in enumerate(zipped):
    print(i, j)  # This will not be invoked as zipped is empty

Zip can be used on any collection: set, tuple, list, etc. One point to note with sets: since a set does not maintain order, you will not be certain which elements form the resulting tuples.


Hands-on Exercises

Exercise 1: Mapping Students to Marks

You have two lists containing matching student details:

  • Student Names: students = ["Alice", "Bob", "Charlie"]
  • Final Grades: grades = [92, 85, 88]

Write a Python program to:

  1. Zip the two lists together using zip().
  2. Convert the resulting zip object into a standard list of tuples.
  3. Print the final list of tuples.
# Write your code below and click Run Code
Click to view Answer
students = ["Alice", "Bob", "Charlie"]
grades = [92, 85, 88]

# Zip and convert to list
student_records = list(zip(students, grades))

print(student_records)
# Output: [('Alice', 92), ('Bob', 85), ('Charlie', 88)]

Exercise 2: Iterating Paired Inventory

You have two lists representing store inventory:

  • Products: products = ["Laptop", "Mouse", "Keyboard"]
  • Stock quantities: stock = [15, 120, 45]

Write a Python program to:

  1. Zip the lists together.
  2. Iterate through the zipped object using a for loop, unpacking the product name and stock quantity in each iteration.
  3. For each pair, print a statement like "Laptop has 15 units in stock".
# Write your code below and click Run Code
Click to view Answer
products = ["Laptop", "Mouse", "Keyboard"]
stock = [15, 120, 45]

# Iterate and unpack zip directly in the loop
for product, quantity in zip(products, stock):
    print(f"{product} has {quantity} units in stock")
Privacy Policy | Terms & Conditions