How to zip lists in Python?

Estimated read time 2 min read

To zip lists in Python, you can use the built-in zip() function. The zip() function takes multiple iterables (such as lists, tuples, or strings) as arguments and returns an iterator that produces tuples containing elements from each iterable, paired together. Here’s an example:

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = [True, False, True]

zipped = zip(list1, list2, list3)

for item in zipped:

In this example, we have three lists: list1, list2, and list3. We pass these lists as arguments to the zip() function, which returns an iterator. We can iterate over this iterator using a for loop and print each tuple.


(1, 'a', True)
(2, 'b', False)
(3, 'c', True)

In each tuple, the first element is from list1, the second element is from list2, and the third element is from list3. If the lists have different lengths, the resulting iterator will be truncated to the length of the shortest list.

You can also convert the zipped iterator to a list or other data structure using the list() function if needed:

zipped_list = list(zip(list1, list2, list3))


[(1, 'a', True), (2, 'b', False), (3, 'c', True)]

The zip() function is a convenient way to combine corresponding elements from multiple lists into tuples or other iterable objects.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply