AppDividend
Latest Code Tutorials

Python Zip Example | Python Zip() Function Tutorial

0

Python Zip Example | Python Zip() Function Tutorial is today’s topic. The zip() function in Python returns the zip object, which is the iterator of tuples where a first item in each passed iterator is paired together, and then a second item in each passed iterator are paired together. Python zip() function take iterables (can be zero or more), makes an iterator that aggregates items based on the iterables passed and returns the iterator of tuples.

Python Zip Example

Python zip function takes the iterable elements as input and returns the iterator. If Python zip function gets no iterable items, it returns the empty iterator. The syntax of the zip() function is following.

zip(*iterables)

# or

zip(iterator1, iterqator2, iterator3 ...)

The zip() function takes:

iterables – can be built-in iterables (like a list, string, dict), or user-defined iterables (an object that has an __iter__ method).

The zip() function returns the iterator of tuples based on an iterable object.

  • If no parameters are passed on zip function then, zip() returns the empty iterator
  • If a single iterable is passed to the zip function then, zip() returns the iterator of 1-tuples. Meaning, the number of items in each tuple is 1.
  • If multiple iterables are passed, ith tuple contains ith Suppose, two iterables are passed; one iterable containing 3 and other containing five elements. Then, a returned iterator has three tuples. It’s because iterator stops when the shortest iterable is exhausted.

Example of Python zip()

Write the following code inside the app.py file.

# app.py

numList = [19, 21, 46]
strList = ['one', 'two', 'three']

outputA = zip(numList, strList)
print(list(outputA))

outputB = zip(strList, numList)
print(list(outputB))

The output is following.

 

Python Zip Example

We can also convert the output to the tuple. See the following code.

# app.py

numList = [19, 21, 46]
strList = ['one', 'two', 'three']

outputA = zip(numList, strList)
print(tuple(outputA))

outputB = zip(strList, numList)
print(tuple(outputB))

The output is following.

 

Python Zip() Function Tutorial

Unzipping the Value Using zip()

We can also extract the data from the Python zip function. If we want to extract the zip, we have to use the same zip()function. But we have to add an asterisk(*) in front of that list you get from the zipped variable.

We can also unzip the numList and strList. See the following example.

# app.py

numList = [19, 21, 46]
strList = ['one', 'two', 'three']

outputA = zip(numList, strList)

x, y = zip(*outputA )
print('numList = ', x)
print('strlist = ', y)

The output is following.

 

Unzipping the Value Using zip()

The * operator can be used in conjunction with zip() to unzip the list. You can use the list() function to get the list from the zipped variable. However, this will return several tuples. The number will differ according to the number of arguments that the zip function took to zip the data.

If the passed iterators have different lengths, the iterator with the least items decides the length of the new iterator. See the following example.

# app.py

numList = [19, 21, 46, 29]
strList = ['one', 'two', 'three']

outputA = zip(numList, strList)

print(list(outputA))

The numList has four items, and strList has three items. So the lengths are not matched. Let’s see the output.

 

zip() function in Python

Zip three iterators

Let’s take an example in which we can use three iterators and then use the zip function on it.

# app.py

numList = [19, 21, 46]
strList = ['one', 'two', 'three']
setList = {'A1', 'B1', 'C1'}

outputA = zip(numList, strList, setList)

print(list(outputA))

The output is following.

 

Zip three iterators

We have taken three different variables and then use the zip() function and then convert into the iterable.

Finally, Python Zip Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.