AppDividend
Latest Code Tutorials

Python Zip Example | Python Zip() Function Tutorial

0

Python zip() function returns the zip object, which is the iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together. Python zip is the container that holds real data inside. Python zip() function takes iterables (can be zero or more), makes an iterator that aggregates items based on the iterables passed and returns the iterator of tuples.

Let’s outline this whole blog post.

Outline of Python zip function

Content Overview

  • Python zip() definition, syntax, and examples.
  • Python zip() function on list.
  • Unzipping values in Python
  • Zip two lists.
  • Zip three iterators.

Now let’s review each step in more detail.

Python zip() Example

Python zip function takes the iterable elements like input and returns the iterator.

If the zip python function gets no iterable items, it returns the empty iterator.

If you are using IPython, then type zip? And check what zip() is about.

If you are not using IPython, then install it: “pip3 install ipython” as I am using Python 3. For Python3 users, pip install ipython will be just fine.

The syntax of the zip() function in Python is the 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, and 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.

#Python zip() list

The zip() function takes the list like the following.

a: a1 a2 a3 a4 a5 a6 a7...
b: b1 b2 b3 b4 b5 b6 b7...
c: c1 c2 c3 c4 c5 c6 c7...

And “zips” them into one list whose entries are 3-tuples (ai, bi, ci). Imagine drawing a zipper horizontally from left to right.

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 the 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 the following.

➜  pyt python3 app.py
((19, 'one'), (21, 'two'), (46, 'three'))
(('one', 19), ('two', 21), ('three', 46))
➜  pyt

#Python zip two lists

In python 3.0, the zip method returns the zip object.

You can get a list out of it by calling list(zip(a, b)).

If you want to merge lists into a list of tuples or zip two lists, then you can use the zip() method. The pythonic approach is the following.

# app.py
listA = [19, 21, 46]
listB = ['one', 'two', 'three']
merge_list = zip(listA, listB)
print(list(merge_list))

See the output.

➜  pyt python3 app.py
[(19, 'one'), (21, 'two'), (46, 'three')]
➜  pyt

You have to know that a zip() function stops at the end of the shortest list, which may not always be what you want in the output.

The itertools module defines a zip_longest() method, which stops at the end of the longest list, filling missing values with something you provide as a parameter.

#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 the following.

➜  pyt python3 app.py
numList =  (19, 21, 46)
strlist =  ('one', 'two', 'three')
➜  pyt

The * operator can be used in conjunction with a 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 elements. So the lengths are not matched. Let’s see the output.

➜  pyt python3 app.py
[(19, 'one'), (21, 'two'), (46, 'three')]
➜  pyt

#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 the following.

➜  pyt python3 app.py
[(19, 'one', 'C1'), (21, 'two', 'A1'), (46, 'three', 'B1')]
➜  pyt

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

Finally, Python zip() function example Tutorial is over.

Recommended Posts

Python ascii() Example

Python bin() Example

Python bool() Example

Python any() Example

Python all() Example

Leave A Reply

Your email address will not be published.

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