Python

Python zip() Function

The zip() function is used to combine two or more iterables together.

It pairs the elements based on their positions.

Basic Syntax

zip(iterable1, iterable2, ...)

Example:

names = ["Ram", "Sita", "Hari"]
marks = [75, 85, 65]

result = zip(names, marks)

print(list(result))

Output:

[('Ram', 75), ('Sita', 85), ('Hari', 65)]

The first item from each iterable is paired together, then the second, and so on.

1. Basic zip() Example

names = ["Ram", "Sita", "Hari"]
marks = [75, 85, 65]

for name, mark in zip(names, marks):
    print(name, mark)

Output:

Ram 75
Sita 85
Hari 65

2. Combine Two Lists

names = ["Ram", "Sita", "Hari"]
cities = ["Butwal", "Kathmandu", "Pokhara"]

for name, city in zip(names, cities):
    print(name, city)

Output:

Ram Butwal
Sita Kathmandu
Hari Pokhara

3. Convert zip() to a List

zip() returns a zip object.

names = ["Ram", "Sita", "Hari"]
marks = [75, 85, 65]

result = zip(names, marks)

print(result)

Output will look similar to:

<zip object at 0x...>

To see the actual values:

result = list(zip(names, marks))

print(result)

Output:

[('Ram', 75), ('Sita', 85), ('Hari', 65)]

4. zip() with Three Lists

You can combine more than two iterables.

names = ["Ram", "Sita", "Hari"]
marks = [75, 85, 65]
cities = ["Butwal", "Kathmandu", "Pokhara"]

for name, mark, city in zip(names, marks, cities):
    print(name, mark, city)

Output:

Ram 75 Butwal
Sita 85 Kathmandu
Hari 65 Pokhara

5. Create a Dictionary Using zip()

This is one of the most common uses of zip().

names = ["Ram", "Sita", "Hari"]
marks = [75, 85, 65]

students = dict(zip(names, marks))

print(students)

Output:

{'Ram': 75, 'Sita': 85, 'Hari': 65}

Here:

names → dictionary keys
marks → dictionary values

6. Create a Dictionary with Two Lists

Another way is dictionary comprehension:

names = ["Ram", "Sita", "Hari"]
marks = [75, 85, 65]

students = {
    name: mark
    for name, mark in zip(names, marks)
}

print(students)

Output:

{'Ram': 75, 'Sita': 85, 'Hari': 65}

This connects zip() with the dictionary comprehension topic you studied earlier.

7. Different Length Lists

Suppose the lists have different lengths:

names = ["Ram", "Sita", "Hari"]
marks = [75, 85]

result = list(zip(names, marks))

print(result)

Output:

[('Ram', 75), ('Sita', 85)]

By default, zip() stops when the shortest iterable is exhausted.

So Hari is not included.

8. zip() with Equal-Length Lists

products = ["Laptop", "Mouse", "Keyboard"]
prices = [80000, 1500, 3000]

for product, price in zip(products, prices):
    print(product, price)

Output:

Laptop 80000
Mouse 1500
Keyboard 3000

9. Practical Student Example

students = ["Ram", "Sita", "Hari", "Gita"]
marks = [75, 85, 65, 90]

for student, mark in zip(students, marks):
    print(f"{student}: {mark}")

Output:

Ram: 75
Sita: 85
Hari: 65
Gita: 90

10. Calculate Total Using zip()

Suppose we have quantities and prices:

quantities = [2, 3, 5]
prices = [100, 50, 20]

for quantity, price in zip(quantities, prices):
    total = quantity * price
    print(total)

Output:

200
150
100

11. Calculate Total Shopping Cost

quantities = [2, 3, 5]
prices = [100, 50, 20]

total = sum(
    quantity * price
    for quantity, price in zip(quantities, prices)
)

print(total)

Output:

450

This combines:

  • zip()
  • Generator expression
  • sum()

12. Compare Two Lists

list1 = [10, 20, 30, 40]
list2 = [10, 25, 30, 50]

for a, b in zip(list1, list2):
    if a == b:
        print(a, "is equal to", b)

Output:

10 is equal to 10
30 is equal to 30

13. Find Differences Between Two Lists

list1 = [10, 20, 30, 40]
list2 = [10, 25, 30, 50]

for a, b in zip(list1, list2):
    if a != b:
        print(a, b)

Output:

20 25
40 50

14. Add Two Lists

numbers1 = [10, 20, 30]
numbers2 = [1, 2, 3]

result = [
    a + b
    for a, b in zip(numbers1, numbers2)
]

print(result)

Output:

[11, 22, 33]

15. Multiply Two Lists

numbers1 = [2, 3, 4]
numbers2 = [5, 10, 2]

result = [
    a * b
    for a, b in zip(numbers1, numbers2)
]

print(result)

Output:

[10, 30, 8]

16. Find Maximum Values

list1 = [10, 50, 30]
list2 = [20, 40, 60]

result = [
    max(a, b)
    for a, b in zip(list1, list2)
]

print(result)

Output:

[20, 50, 60]

17. Find Minimum Values

list1 = [10, 50, 30]
list2 = [20, 40, 60]

result = [
    min(a, b)
    for a, b in zip(list1, list2)
]

print(result)

Output:

[10, 40, 30]

18. zip() with enumerate()

You can combine zip() and enumerate().

names = ["Ram", "Sita", "Hari"]
marks = [75, 85, 65]

for number, (name, mark) in enumerate(
    zip(names, marks),
    start=1
):
    print(number, name, mark)

Output:

1 Ram 75
2 Sita 85
3 Hari 65

This is useful for displaying numbered records.

19. zip() with Strings

letters = ["A", "B", "C"]
numbers = [1, 2, 3]

result = list(zip(letters, numbers))

print(result)

Output:

[('A', 1), ('B', 2), ('C', 3)]

20. Zip Two Strings

Strings themselves are iterable.

word1 = "ABC" word2 = "123"

result = list(zip(word1, word2))

print(result)

Output:

[('A', '1'), ('B', '2'), ('C', '3')]

21. Practical Course Example

courses = [
    "Python",
    "Django",
    "MERN",
    "Data Science"
]

durations = [
    "8 Weeks",
    "10 Weeks",
    "12 Weeks",
    "12 Weeks"
]

for course, duration in zip(courses, durations):
    print(course, "-", duration)

Output:

Python - 8 Weeks
Django - 10 Weeks
MERN - 12 Weeks
Data Science - 12 Weeks

22. Practical Course Price Example

courses = [
    "Python",
    "Django",
    "MERN",
    "Data Science"
]

prices = [
    15000,
    18000,
    22000,
    25000
]

for course, price in zip(courses, prices):
    print(f"{course}: Rs. {price}")

Output:

Python: Rs. 15000
Django: Rs. 18000
MERN: Rs. 22000
Data Science: Rs. 25000

23. Filter Using zip()

You can use conditions while processing multiple lists.

names = ["Ram", "Sita", "Hari", "Gita"]
marks = [75, 35, 65, 90]

passed = [
    name
    for name, mark in zip(names, marks)
    if mark >= 40
]

print(passed)

Output:

['Ram', 'Hari', 'Gita']

24. Get Failed Students

names = ["Ram", "Sita", "Hari", "Gita"]
marks = [75, 35, 65, 90]

failed = [
    name
    for name, mark in zip(names, marks)
    if mark < 40
]

print(failed)

Output:

['Sita']

25. Find the Highest Scorer

names = ["Ram", "Sita", "Hari", "Gita"]
marks = [75, 85, 65, 90]

highest = max(
    zip(marks, names)
)

print(highest)

Output:

(90, 'Gita')

Here max() compares the marks first.

You can display it more clearly:

marks = [75, 85, 65, 90]
names = ["Ram", "Sita", "Hari", "Gita"]

highest_mark, highest_student = max(
    zip(marks, names)
)

print(highest_student)
print(highest_mark)

Output:

Gita
90

26. Transpose a Matrix

zip() can also be used to transpose rows and columns.

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

result = list(zip(*matrix))

print(result)

Output:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

The * unpacks the rows into separate arguments for zip().

27. Unzip Data

Suppose we have:

students = [
    ("Ram", 75),
    ("Sita", 85),
    ("Hari", 65)
]

We can separate the values:

names, marks = zip(*students)

print(names)
print(marks)

Output:

('Ram', 'Sita', 'Hari')
(75, 85, 65)

So zip() can be used for both zipping and unzipping data.

28. zip() with Dictionary Items

students = {
    "Ram": 75,
    "Sita": 85,
    "Hari": 65
}

for name, mark in zip(
    students.keys(),
    students.values()
):
    print(name, mark)

Output:

Ram 75
Sita 85
Hari 65

However, simply using:

for name, mark in students.items():
    print(name, mark)

is cleaner in this particular situation.

29. zip() with a Generator

zip() works with generators too.

numbers = (
    x * 2
    for x in range(1, 6)
)

letters = ["A", "B", "C", "D", "E"]

for number, letter in zip(numbers, letters):
    print(number, letter)

Output:

2 A
4 B
6 C
8 D
10 E

30. Practical Data Processing Example

products = ["Laptop", "Mouse", "Keyboard"]
prices = [80000, 1500, 3000]
quantities = [1, 2, 3]

for product, price, quantity in zip(
    products,
    prices,
    quantities
):
    total = price * quantity

    print(
        product,
        "=", total
    )

Output:

Laptop = 80000
Mouse = 3000
Keyboard = 9000

zip() vs enumerate()

These two functions are often confused.

enumerate()

Used when you need:

index + value

Example:

for index, value in enumerate(names):
    print(index, value)

zip()

Used when you need:

value from one iterable + value from another iterable

Example:

for name, mark in zip(names, marks):
    print(name, mark)

Important Points to Remember

Basic syntax

zip(iterable1, iterable2)

Two lists

zip(names, marks)

Three lists

zip(names, marks, cities)

Convert to list

list(zip(names, marks))

Create dictionary

dict(zip(names, marks))

With comprehension

[
    name
    for name, mark in zip(names, marks)
    if mark >= 40
]

Different-length iterables

By default, zip() stops at the shortest iterable.

Main idea

List 1       List 2
  ↓            ↓
 Ram          75
 Sita         85
 Hari         65
  ↓            ↓
      zip()
        ↓
(Ram, 75)
(Sita, 85)
(Hari, 65)

Easy way to remember

enumerate() → index + value

zip()       → value + value

map()       → transform values

filter()    → select values
Interactive Sandbox
Python