What is the inverse function of zip?

Learn about the inverse function of zip function in Python? By Pranit Sharma Last updated : October 07, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

Python zip() function

Python zip() method is used to return a single object, having mapped values from all the containers. It takes some sort of iterables or containers and it is used to map the similar index of multiple containers so that they can be used just using a single entity.

Here, suppose we have used the zip() function from the NumPy library to sort tuples and now we have a list containing all the tuples. We have since modified that list and now we need to restore the tuples so we can use our data.

Inverse function of zip

For this purpose, we need to use the (*) operator with the zip() function result to reverse this result so that if the zip() method gives us zipped_data, *zipped_data given the inverse if zip().

Let us understand with the help of an example,

Python program to demonstrate the example of inverse function of zip

# Import numpy
import numpy as np

# Creating two lists
a = [1,2,3]
b = [4,5,6]

# Display original lists
print("Original List 1:\n",a,"\n")
print("Original List 2:\n",b,"\n")

# Using zip
Zipped = zip(a,b)

# Display zipped data
print("Zipped data:\n",Zipped,"\n")

# Unzipping
res = zip(*Zipped)

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: What is the inverse function of zip?

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.