Table of Contents
In this tutorial, we will learn about the Python zip() function with the help of examples.
The zip() function takes iterables (can be zero or more), aggregates them in a tuple, and returns it.
Example
languages = ['Java', 'Python', 'JavaScript'] versions = [14, 3, 6] result = zip(languages, versions) print(list(result))
1. Syntax of zip()
The syntax of the zip() function is:
zip(*iterables)
2. zip() Parameters
| Parameter | Description |
|---|---|
iterables | can be built-in iterables (like: list, string, dict), or user-defined iterables |
Recommended Reading: Python Iterators, __iter__ and __next__
3. zip() Return Value
The zip() function returns an iterator of tuples based on the iterable objects.
- If we do not pass any parameter,
zip()returns an empty iterator - If a single iterable is passed,
zip()returns an iterator of tuples with each tuple having only one element. - If multiple iterables are passed,
zip()returns an iterator of tuples with each tuple having elements from all the iterables.
Suppose, two iterables are passed tozip(); one iterable containing three and other containing five elements. Then, the returned iterator will contain three tuples. It’s because the iterator stops when the shortest iterable is exhausted.
4. Example 1: Python zip()
number_list = [1, 2, 3] str_list = ['one', 'two', 'three'] # No iterables are passed result = zip() # Converting iterator to list result_list = list(result) print(result_list) # Two iterables are passed result = zip(number_list, str_list) # Converting iterator to set result_set = set(result) print(result_set)
Output
[]
{(2, 'two'), (3, 'three'), (1, 'one')}
5. Example 2: Different number of iterable elements
numbersList = [1, 2, 3]
str_list = ['one', 'two']
numbers_tuple = ('ONE', 'TWO', 'THREE', 'FOUR')
# Notice, the size of numbersList and numbers_tuple is different
result = zip(numbersList, numbers_tuple)
# Converting to set
result_set = set(result)
print(result_set)
result = zip(numbersList, str_list, numbers_tuple)
# Converting to set
result_set = set(result)
print(result_set)
Output
{(2, 'TWO'), (3, 'THREE'), (1, 'ONE')}
{(2, 'two', 'TWO'), (1, 'one', 'ONE')}
The * operator can be used in conjunction with zip() to unzip the list.
zip(*zippedList)
6. Example 3: Unzipping the Value Using zip()
coordinate = ['x', 'y', 'z']
value = [3, 4, 5]
result = zip(coordinate, value)
result_list = list(result)
print(result_list)
c, v = zip(*result_list)
print('c =', c)
print('v =', v)
Output
[('x', 3), ('y', 4), ('z', 5)]
c = ('x', 'y', 'z')
v = (3, 4, 5)
Related posts:
Python Program to Sort Words in Alphabetic Order
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Print Colored Text to the Terminal
Python Program to Count the Number of Occurrence of a Character in String
Python String center()
Python Program to Differentiate Between type() and isinstance()
Python Program to Get a Substring of a String
Python Anonymous / Lambda Function
Python sleep()
Python Program to Convert Decimal to Binary Using Recursion
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Find All File with .txt Extension Present Inside a Directory
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Input, Output and Import
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Deep Learning in Python - LazyProgrammer
Python Program to Find HCF or GCD
Python Program to Split a List Into Evenly Sized Chunks
Python String startswith()
Python datetime
Python Shallow Copy and Deep Copy
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Display Powers of 2 Using Anonymous Function
Python float()
Python Program to Randomly Select an Element From the List
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Decorators
Python Program to Get the Class Name of an Instance
Python setattr()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String find()
Python open()