How to Use a Zipped list in Python
The zip()
function in Python is a powerful tool for combining multiple iterables into a single iterable of tuples.
This guide explores how to use zip()
to create zipped lists, how to print them, and techniques for customizing the output. We'll cover converting zip objects to lists and strategies for working with lists instead of tuples as output.
Printing Zipped Lists
The zip()
function takes multiple iterables as input and returns an iterator of tuples, where each tuple contains corresponding elements from the input iterables. To print a zipped list, you need to convert this iterator into a list.
list_1 = [1, 2, 3]
list_2 = ['tutorial', 'reference', 'com']
zipped_list = list(zip(list_1, list_2))
print(zipped_list) # Output: [(1, 'tutorial'), (2, 'reference'), (3, 'com')]
Understanding Zip Objects and Iterators
It's important to understand that zip()
returns a zip object, which is an iterator. Iterators produce values one at a time when requested, making them memory-efficient. This is why you need to explicitly convert the zip object to a list using list()
if you want to see all the elements at once or access them multiple times.
list_1 = [1, 2, 3]
list_2 = ['tutorial', 'reference', 'com']
zip_object = zip(list_1, list_2)
print(zip_object) # Output: <zip object at 0x...>
zipped_list = list(zip_object)
print(zipped_list) # Output: [(1, 'tutorial'), (2, 'reference'), (3, 'com')]
Trying to print the zip_object
directly won't show you the contents. To display or use the zipped data, always convert it to a list first. Also, once a zip object/iterator has been consumed (iterated through), it is exhausted and can not be used again. That's why in the example above we create a new zip
object to convert to a list.
Zipping Multiple Iterables
zip()
can handle more than two iterables. The resulting tuples will have the same number of elements as the number of input iterables:
list_1 = [1, 2, 3]
list_2 = ['tutorial', 'reference', 'com']
list_3 = ['one', 'two', 'three']
zipped_list = list(zip(list_1, list_2, list_3))
print(zipped_list) # Output: [(1, 'tutorial', 'one'), (2, 'reference', 'two'), (3, 'com', 'three')]
Creating Lists of Lists from Zipped Output
Sometimes, you might need a list of lists instead of a list of tuples. Here are two common ways to achieve this:
1. Using List Comprehensions
List comprehensions provide a concise way to transform each tuple from the zip
object into a list.
list_1 = [1, 2, 3]
list_2 = ['a', 'b', 'c']
list_of_lists = [list(tup) for tup in zip(list_1, list_2)]
print(list_of_lists) # Output: [[1, 'a'], [2, 'b'], [3, 'c']]
2. Using the map()
Function
The map()
function applies a function (in this case, list()
) to each item in an iterable (the zip
object).
list_1 = [1, 2, 3]
list_2 = ['a', 'b', 'c']
list_of_lists = list(map(list, zip(list_1, list_2)))
print(list_of_lists) # Output: [[1, 'a'], [2, 'b'], [3, 'c']]
Both the list comprehension and map()
approaches achieve the same result. List comprehensions are often considered more Pythonic and readable for simple transformations.