Updated May 24, 2023
Definition of NumPy zip
As the name suggests, it zipped the variable together. We can use iterable objects with this function, like an array, list, string, dictionary, etc. This function pairs the first value of every iterable object and proceeds ahead like this only. This function is also available inside the NumPy library, which is used for computation on the array and single element. It is not mandatory to have an iterable object of the same type.
Syntax:
We can call this function using the ‘zip’ keyword, and it takes any itearble object inside it as the input parameter. Below see the syntax to use the zip function in Python;
zip(object1, object2, object3, object4,.. so on)
Also, see the syntax as per the Python doc;
zip(*iterators)
This function takes containers or an iterable object like; string, list, array, etc.
Let’s see one practice syntax for a better understating of the function see below. We will discuss its working in detail in the next section;
arr1 = [val1, val2, val3]
arr2 = [val1, val2, val3]
result = zip(arr1, arr2)
How does zip Function Work in NumPy?
Now we know that we use this function to zip or group the variable of different arrays. Sometimes, it just removes the need for creating the user object. It functions the same way as its name, zipping different arrays or listing variable values together. This can take any number of an iterable object, also the length of the iterable object is also not a constraint here. If we talk about its return type, it returns single objects which contain all the values zipped from the different iterable objects. We can use this function in many places, like if we want to amp the student data based on the name, city, marks, and roll number, so we can use this function without creating a class. Let’s take one example to understand its syntax in more detail;
- Return Type: This function returns a single object. This single object contains all the variables from all the array or iterable objects we pass into the function.
- Parameter: This function takes an iterable object of any length and any number of parameters.
Let’s see one example to understand its working in a better way;
arr1 = [ "amit", "sumit", "vishal", "mita", "geets", "sujata", “kavita”, ”lalita”, ”somore” ]
arr2 = [ 001, 002, 003, 004, 005, 006, 007, 008, 009, 010, so on …]
result = zip(arr1, arr2)
In the above example, we are creating two arrays. The first array contains the name of the student and the second array contains the roll number associated with the student. If we talk about any other programming language, then we need an object which will bind both the variable of the student class or else we need to create a map that will map the value of the student as key-value pair. This will require a lot of code. But in Python, we have a zip() function which will bind the variable of two or more different arrays into one single array. In the last line, we are calling the zip() function and passing both the array into it as a parameter. We are holding the data into the result variable here, which will be a zipped iterable object containing the result. Now we will talk about the real-time scenario in which we cause it.
Suppose we have a student to whom we must bind the data according to roll number, marks, city, and address. We can use this function for this. We need to create three arrays that will contain the name, roll number, marks, city, and address of the student, respectively. After that, we can pass this input parameter in the zip() function. So in the output, we will have all the student data mapped with the corresponding record of the student.
Example:
studentName = [];
rollNumber = [];
city =[];
address = [];
result = zip(studentName, rollNumber, city, address)
So, in the end, it will generate the object which will be going to contain the mapped object of the student object.
Pints to be remembered while working with the zip() function in Python;
- Like we zip our object into a single object, we can also perform unzip of the object. This operation can be performed by using the unzip() function.
- zip() function does not bond to the length of the iterable object. The same length is not mandatory.
Examples of NumPy zip
Following are the examples are given below:
Example #1
In this example, we are creating two iterable objects and just calling the zip function on them. This is for beginners to show the working.
Code:
print("demo for zip function")
#creating array object
arr1 = [ "value1", "value2", "value3", "value4", "value5", "value6" ]
arr2 = [ 1, 2, 3, 4, 5, 6 ]
#calling zip function here to bind two array elements
result = zip(arr1, arr2)
#here mapping result to print in formte
result = set(result)
#printing result
print("result after zip function is ::")
print(result)
Output:
Example #2
In this example, we will map the student data for name, roll number, city, and grade they obtained and print the result.
Code:
print("demo for zip function")
#creating array object
arr1 = [ "Amit", "Sumit", "Vinit", "Kavit", "aman", "vijay" ]
arr2 = [ "mumbai", "pune", "bng", "chennai", "hyd", "deilhi" ]
arr3 = [ "001", "004", "003", "009", "010", "011" ]
arr4 = [ 1, 2, 3, 4, 5, 6 ]
#calling zip function here to bind two array elements
result = zip(arr1, arr2, arr3, arr4)
#here mapping result to print in formte
result = set(result)
#printing result
print("result after zip function is ::")
print(result)
Output:
Conclusion
If we want to bind or zip the data of different arrays, then we can go for the zip function in Python of NumPy. This function eliminates the need for a class object if not required. We can easily map our data with any number of the array, and this can be done very easily with the use of the zip() function.
Recommended Articles
We hope that this EDUCBA information on “NumPy zip” was beneficial to you. You can view EDUCBA’s recommended articles for more information.