How To Zip Two Or Multiple Lists Together In Python

How do you zip two lists together in Python? What if you want to zip multiple lists together, is it the same process or something different?

The built-in function zip() allows users to combine iterables, such as a list, into tuples by taking each corresponding item from the lists passed into its parameters merging the lists into one. It does not matter whether you have two or multiple lists the function are the list parameters are the same.

For example, suppose you have the following lists and you want to combine them all together:

>>> first_names = ["Joe", "Jane", "Mark"]
>>> middle_initials = ["A", "B", "C"]
>>> surnames = ["Bloggs", "Doe", "Smith"]
>>> zip(first_names, middle_initials, surnames)
<zip object at 0x100e535c0>

As you can see the result from the zip() function as displayed in the REPL output above is a zip object. You can inspect each element in the object by printing it, so instead of just outputting the result run it through a for loop, perhaps something like:

>>> for item in zip(first_names, middle_initials, surnames):
...     print(item)
...
('Joe', 'A', 'Bloggs')
('Jane', 'B', 'Doe')
('Mark', 'C', 'Smith')

If you wanted to wrap the result of the tuples into a list you could wrap the zip() function into a list() function like so:

>>> first_names = ["Joe", "Jane", "Mark"]
>>> middle_initials = ["A", "B", "C"]
>>> surnames = ["Bloggs", "Doe", "Smith"]
>>> list(zip(first_names, middle_initials, surnames))
[('Joe', 'A', 'Bloggs'), ('Jane', 'B', 'Doe'), ('Mark', 'C', 'Smith')]

As you can see from the above result the output is now nice and neatly entered into a list that can be further referenced in your code.

While these lists have been separated out into their own individual lists, what if the original list was a two-dimensional nested list?

How Do You Zip A Two-Dimensional List In Python?

Can you zip all the lists within a two-dimensional list using the zip() function?

Suppose you had the same type of data but the list was two-dimensional and structured like so:

>>> my_list = [["Joe", "Jane", "Mark"],
...            ["A", "B", "C"],
...            ["Bloggs", "Doe", "Smith"]]

If you were to zip this two-dimensional list as is, it would produce just a list of tuples for each row:

>>> list(zip(my_list))
[(['Joe', 'Jane', 'Mark'],), (['A', 'B', 'C'],), (['Bloggs', 'Doe', 'Smith'],)]

How can you produce the same result as I did above when each list was contained in its own variable?

The way you can break up the two-dimensional list is by applying the * operator to the variable – this operator unpacks the contents of the variable, here’s a simple demonstration of this operation:

>>> print(*my_list, sep='\n')
['Joe', 'Jane', 'Mark']
['A', 'B', 'C']
['Bloggs', 'Doe', 'Smith']

As you can see from the above output when printing the contents of the list and using the sep (separator) parameter (set to the new line separator \n ) to separate each item being unpacked from the * (asterisk operator) I get each individual row from the original two-dimensional list.

Knowing this handy operator and how it can help to unpack each row in the original two-dimensional list helps to make each row act like its own individually inserted row into the zip() function.

Trying that out using the zip() function would look something like this:

>>> my_list = [["Joe", "Jane", "Mark"],
...            ["A", "B", "C"],
...            ["Bloggs", "Doe", "Smith"]]
>>> list(zip(*my_list))
[('Joe', 'A', 'Bloggs'), ('Jane', 'B', 'Doe'), ('Mark', 'C', 'Smith')]

As you can see from the above code you can achieve the exact same result as you normally would have by passing each row individually into the zip() function.

Can You Zip Lists That Aren’t The Same Size?

How do you handle zipping two or more lists where the contents of one list, or all lists, are not of the same length?

If you have lists of varying lengths and you try to zip them all up using the zip() function you will get the smallest length from combining all elements in the list together.

For example, if I used the same variables as before, but removed the last element on the middle_initials list then the output will be just the first and second elements combined together, as seen here:

>>> first_names = ["Joe", "Jane", "Mark"]
>>> middle_initials = ["A", "B"]
>>> surnames = ["Bloggs", "Doe", "Smith"]
>>> list(zip(first_names, middle_initials, surnames))
[('Joe', 'A', 'Bloggs'), ('Jane', 'B', 'Doe')]

Therefore, you do need to be mindful of the contents of your lists when zipping them up – if one list’s contents are shorter than the rest then the result will be the length of the shorter list.

Summary

Python allows you to zip two or more lists into one by using the handy zip() function.

An easy way to zip two-dimensional lists is to use the asterisk operator which unpacks each of the individual lists within the original two-dimensional list and enables the zip() function to operate on each list as if were being passed through as individual parameters to the function.

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.