| by Arround The Web | No comments

## NumPy Zip

“As the name suggests, NumPy is Numerical Python, and it is an advanced library (it’s a chunk of small modules) of python that will work with arrays, strings, and lists. NumPy is used in Artificial Intelligence. In python, if we compile any array or list, the data will be stored dynamically, but in NumPy, the data will be stored sequentially. Because of NumPy, our work will be faster as compared to basic python.

The zip() is one of the functions of the NumPy library. The NumPy zip() function is used to zip the variables together in an object or container. This function works with multiple iterable objects or containers like an array, lists, and strings. In this function, the first value in each passed iterable object is paired together with the second value in each passed iterable object is paired together and so on. If the length of iterable objects has different lengths, the iterator object which has the least length decides the length of the new iterator object.”

## Syntax

Here is the syntax of the NumPy zip() function, where this function works with multiple iterable objects simultaneously. We use the zip keyword to call this function, and it takes a string, array, and lists as an iterable object inside it as the input variable.

## Parameter Value

The zip() function contains the iterable objects that contain any number of parameters with different lengths. After compiling this function, it only returns a single object because this function contains all the variables of the multiple iterable objects.

## Example 01: zip() Function

Let’s look at the very first and simple example of the NumPy zip() function. In this example, we have declared two tuples identified as “Object1” and “Object2”. We assigned a string to these tuples, the “Object1” have (“Angelo”, “Chris”, “Mona”, “Cristiano”) and the “Object2” have (“Mathews”, “Jorden”, “Lisa”, “Ronaldo”). We then created the new tuple named “Object,” which contains the parameters of “Object1” and “Object2,” and then we zipped them together. We then make use of the print() function to print the tuple “Object”.

Object1 = ("Angelo", "Chris", "Mona", "Cristiano")

Object2 = ("Mathews ", "Jorden", "Lisa", "Ronaldo")

Object = zip(Object1, Object2)

print (tuple(Object))

Here is the output after compiling the above code snippet.

## Example 02: Tuples Having Different Lengths

In the above code snippet, we have declared the two tuples with the same length. Now let’s see what if we have tuples of different lengths. As you see below, we added string parameters in both tuples, but the tuple “Object2” has the least length in both tuples, so the length of the new tuple “Object” decided by the “Object2” means the length of “Object” is same as “Object2”.

Object1 = ("Angelo", "Chris", "Mona", "Cristiano", "Chris", "Mitchell Starc")

Object2 = ("Mathews", "Jorden", "Lisa", "Ronaldo", "Gayle")

Object = zip(Object1, Object2)

print (tuple(Object))

## Example 03: Pass Only One Tuple

Let’s try another example when we have only one tuple, “Object1,” containing string parameters (“Angelo”, “Chris”, “Mona”, “Cristiano”, “Chris”). When we pass only a single iterable tuple, it will return the iterator 1-tuples. We then create a new tuple named “Object” that contains the zip() function that was applied to the tuple “Object1”.

Object1 = ("Angelo", "Chris", "Mona", "Cristiano", "Chris")

Object = zip(Object1)

print (tuple(Object))

After compiling the above code snippet, we get this relatable output:

## Example 04: Passing No Parameters

Let us look at another example where we haven’t passed any tuple to the zip() function. As you see below in “Object1,” we didn’t pass any parameters. Then we applied the zip() function to the “Object,” and then I printed it.

Object1 = ()

Object = zip(Object1)

print (tuple(Object))

Here is the output of the above code. When we call the zip() function, we get an empty tuple.

## Example 05

Now we have another simpler example where we calculate the tax of each item. The illustration below demonstrates how to use the zip() Function in Numpy to combine three lists and carry out the desired output. In the first line of code, we have to use the print() to display the message in the output “Calculating Tax On Each Item”. This string or message tells us that we are calculating the tax on each item in the list of items.

Then, we create 3 tuples list that is “Items”, “Price”, and “Tax”. We have assigned string values “[“Sandals”, “Bottom”, “Perfume”, “Earings”, “Dress”]” to the “Items” tuple list, and on the other hand, we assigned int values” [10000, 64000, 35700, 26700, 83900]” to tuple “Price” and the tuple “Tax” having a list of “[5, 10, 15, 20, 30]”.

print("Calculating Tax On Each Item:\n")

Items = ["Sandals", "Bottom", "Perfume", "Earings", "Dress"]

Price = [10000, 64000, 35700, 26700, 83900]

Tax = [5, 10, 15, 20, 30]

Zip = list(zip(Items, Price, Tax))

for Items,Price,Tax in Zip:

Total_Tax=Price*Tax/100

Total_Amount=Total_Tax+Price

print("Items:{}    Price:{}    Tax:{}%    Total_Amount:{}".format(Items,Price,Tax,Total_Amount))

Now we have applied the zip() function to “Items”, “Price”, and “Tax”. And then we applied the list() method to the zip() function, and then it will be stored into another tuple, “Zip”. After creating tuples, we used “for loop” to display the data list, which was stored in the “Items”, “Price”, and “Tax”. In the for loop, we have calculated “Tax” on each item by applying the tax formulae “Price*Tax/100” and stored the result into another variable, “Total_Tax”.

Now we have to calculate the “Total_Amount” of each item, so we have added “Total_Tax” and “Price” and stored the result into another variable, “Total_Amount”. To demonstrate the required output, we use the print() function. In print (), we display “Items”, their “Price”, “Tax” that was implemented according to the Item, and “Total_Amount” of items. And then, we applied the format() method that is used to format the “Items”, “Price”, “Tax”, and “Total_Amount” values and insert the specified data inside these tuples.

Here is the needed output of the above illustration. As you see, we have multiple items with different prices, and on each item, a different tax is applied to it. We have different amounts for each item because tax is different for each item.

## Conclusion

In this editorial, we have studied what NumPy is and why we used the NumPy library. Then we discussed one of the functions of NumPy, which is the NumPy zip() function. We have learned what the zip() function is and why we used the zip() function in NumPy. We have also looked at the syntax and returned parameters of the NumPy zip() function. Then we performed multiple examples with its code implementation so that no point of confusion would be left. I hope this guide will be helpful in your learning and understanding of the zip() function of Numpy.

Source: linuxhint.com