What is Python Zip?
Python zip is a built-in Python function that allows us to create an iterator of tuples by mapping together elements from iterable based on their index numbers.
How to use the Python Zip function?
For example, we can use the Python zip function to create another zip object that will combine elements from the two lists. In this case, we will need to create the two lists that we intend to zip together, and then create a variable that will store our new zipped object.
We are going to call this object zipped and then to apply the zip function to the two lists that we just created we will need to use the word zip followed by a pair of parentheses and then pass both lists that we want to be zipped between the parentheses separated by a comma.
If we print out the variable zipped we will end up with a zip object in the console or terminal.
Printing out the elements of a zipped object
In order to get the actual list values of the zip object, we need to convert the object to a list. Now the Python list method list() takes sequence types and converts them to lists.
We can see in the output that the zip function takes elements that fall in the same index from both lists and creates a tuple for each one of them. For instance ‘elephant’ and ‘4’ fall within the same index and therefore have been placed in the same tuple.
This is also the case for the rest of the elements in both iterables. So ‘tiger’ and ‘6’ will also be paired together in one tuple.
Now we should, however, note that the standard zip function does not work when we have lists or tuples with mismatched lengths i.e if one list is longer than the other.
Using zip function on lists
So for example we can add a fourth item to the list of animals so that it now contains: [‘elephant’, ‘tiger’, ‘lion’, ‘monkey’] and on the other hand maintain the qty list unchanged.
If we try to run the zip function on the two lists once again we would note that the function will ignore the elements that come after the length of the shorter list.
Now we can import zip_longer function from Iter tools which are also built into Python and use a fill value if we want to include all the elements in the long list. However, that is not covered here.
Return a set with Python Zip
We can also return a set instead of a list by simply replacing the list() method with the set() method instead. This will return a set of tuples instead of a list of tuples.
Unpack Zip elements
If we wish to unpack the zip elements we will have to convert the list object into a list. Since there is no unzip function, we need to use the argument given to us.
We are going to create two variables to store the values that we are going to unpack in their respective lists as they were initially. We are going to equate the two variable names to the Python zip function that takes in the new_zip variable storing the list of tuples zipped together.
Note that this time since we are unpacking elements already zipped together we need to add a star just before the *new_zip variable that is storing the already zipped object. This is going to basically reverse the process and unzip the zipped object so that when we run the code below we should get the original lists that we began with.
Summary
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
