How to Print a Zipped list in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Print a zipped list in Python
  2. Zip with List output instead of Tuple in Python

# Print a zipped list in Python

To print a zipped list:

  1. Use the zip() function to get an iterator of tuples.
  2. Use the list() class to convert the iterator to a list.
  3. Use the print() function to print the list.
main.py
list_1 = [1, 2, 3] list_2 = ['bobby', 'hadz', 'com'] my_list = list(zip(list_1, list_2)) # ๐Ÿ‘‡๏ธ [(1, 'bobby'), (2, 'hadz'), (3, 'com')] print(my_list)

print zipped list in python

The code for this article is available on GitHub

The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.

The zip function returns an iterator of tuples.

main.py
list_1 = [1, 2, 3] list_2 = ['bobby', 'hadz', 'com'] zip_obj = zip(list_1, list_2) # ๐Ÿ‘‡๏ธ <zip object at 0x7fb7621fe080> print(zip_obj)

This is why we had to use the list() class to convert the zip object to a list.

main.py
list_1 = [1, 2, 3] list_2 = ['bobby', 'hadz', 'com'] zip_obj = list(zip(list_1, list_2)) # ๐Ÿ‘‡๏ธ [(1, 'bobby'), (2, 'hadz'), (3, 'com')] print(zip_obj)

You can use the zip() function with as many iterables as necessary.

main.py
list_1 = [1, 2, 3] list_2 = ['bobby', 'hadz', 'com'] list_3 = ['one', 'two', 'three'] zip_obj = list(zip(list_1, list_2, list_3)) # ๐Ÿ‘‡๏ธ [(1, 'bobby', 'one'), (2, 'hadz', 'two'), (3, 'com', 'three')] print(zip_obj)

The zip() function returns a zip object of tuples.

# Zip with list output instead of Tuple in Python

To get list output instead of tuple from the zip() function:

  1. Use a list comprehension to iterate over the zip object.
  2. Use the list() class to convert each tuple to a list.
  3. The new list will contain nested lists instead of tuples.
main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] list_of_lists = [list(tup) for tup in zip(list_1, list_2)] # ๐Ÿ‘‡๏ธ [[1, 'a'], [2, 'b'], [3, 'c']] print(list_of_lists)

zip with list output instead of tuple

The code for this article is available on GitHub

The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.

By default, the zip function returns an iterator of tuples.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] # ๐Ÿ‘‡๏ธ [(1, 'a'), (2, 'b'), (3, 'c')] print(list(zip(list_1, list_2)))

We used a list comprehension to iterate over the zip object.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we used the list() class to convert the tuple to a list.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] list_of_lists = [list(tup) for tup in zip(list_1, list_2)] # ๐Ÿ‘‡๏ธ [[1, 'a'], [2, 'b'], [3, 'c']] print(list_of_lists)

The list class takes an iterable and returns a list object.

Alternatively, you can use the map() function.

# Zip with list output instead of Tuple using map()

This is a two-step process:

  1. Use the map() function to pass each tuple in the zip object to the list() class.
  2. Use the list() class to convert the map object to a list.
main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] list_of_lists = list(map(list, zip(list_1, list_2))) # ๐Ÿ‘‡๏ธ [[1, 'a'], [2, 'b'], [3, 'c']] print(list_of_lists)

zip with list output instead of tuple using map

The code for this article is available on GitHub

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The map() function passes each tuple in the zip object to the list() class.

The map() function returns a map object, so we had to use the list() class to convert it to a list.

Which approach you pick is a matter of personal preference. I'd go with using a list comprehension because I find it more explicit and easier to read.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev