What is Zip and UnZip Function in Python - NareshIT
What is Zip and UnZip Function in Python -NareshIT

What is Zip and UnZip Function in Python - NareshIT


Introduction:

  1. Guido van Rossum designed Python , a prominent computer language, which was published in 1991.
  2. Python is a more sophisticated programming language that provides excellent capabilities for data crunching and preprocessing, as well as complicated scientific data analysis and modeling.
  3. Today, Python has other implementations, including Jython, which is written in Java for the Java Virtual Machine.
  4. IronPython is a new variant built in C# for the Common Language Infrastructure, while the PyPy version was written in RPython and translated into C.
  5. The majority of Python modules use a community development model and are open-source and free.?

Zip Function in Python:

  1. The zip() method in Python basically returns a zip object.
  2. The zip object functions primarily as a tuple iterator.
  3. The iterator pairs the first item or value from each passed iterator, followed by the second item from each passed iterator, and so on.
  4. If the given iterators have different lengths, the one with the fewest elements determines the length of the new iterator.

Syntax:

zip(iterator1, iterator2, iterator3 ...)

Note: ?Here the Iterator objects that will be joined together.

Example:

The following example demonstrates how the zip function is used in Python. It should be noticed that if one tuple includes more entries, they are disregarded.

a = ("John",?"Charles",?"Mike") b = ("Jenny",?"Christy",?"Monica",?"Vicky") x =?zip(a, b)

Zip in Python 3:

?Python launched version 3.0, sometimes known as Python 3, in December 2008. Technically, this version was created primarily to address serious issues identified in Python 2. As you are aware, Python 3 was incompatible with Python 2 in a number of ways. It is not backward compatible, and certain Python 3 capabilities have been backported to Python 2.x versions to facilitate the transfer to Python 3.

When it comes to the zip function utility, Python 3 has few variations in terms of syntax, but there are several in terms of values. Let us analyze the following example that will show you how the zip The method is applied in Python 3..

Syntax: zip(*iterators) Parameters: Here the Python iterables or containers that we used to have are ( list, string etc ). Return Value : It is used to returns a single iterator object which is used to contains the mapped values from all the containers which are present.

Example:

# Python code to demonstrate the working of?zip()?

# initializing lists?

name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]?

roll_no = [ 4, 1, 3, 2 ]?

marks = [ 40, 50, 60, 70 ]?

# using zip() to map values?

mapped = zip(name, roll_no, marks)???

# converting values to print as set?

mapped = set(mapped)??

# printing resultant values??

print ("The zipped result is : ",end="")?

print (mapped)?

Note: If we compare Python2 with Python3, we will discover that they are similar in the majority of scenarios.

Unzipping in Python:

Unzipping, like zip(), has been implemented. In Python , the Unzipping process involves turning the zipped values back to their original state before using the zip() function. This is accomplished with the use of the "*" operator.

Consider the following example, which will demonstrate how unzipping works. Here, I will use the same example as in the zip() implementation to help you understand zip() and unzip().

Example:

# Python code to demonstrate the working of? unzipping?

# First we need to initialise the lists???

name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]?

roll_no = [ 4, 1, 3, 2 ]?

marks = [ 40, 50, 60, 70 ]?

# using zip() to map values?

mapped = zip(name, roll_no, marks)???

# converting values to print as list?

mapped = list(mapped)?

# printing resultant values??

print ("The zipped result is : ",end="")?

print (mapped)???

print("\n")??

# unzipping values?

namz, roll_noz, marksz = zip(*mapped)???

print ("The unzipped result: \n",end="")???

# printing initial lists?

print ("The name list is : ",end="")?

print (namz)?

print ("The roll_no list is : ",end="")?

print (roll_noz)???

print ("The marks list is : ",end="")?

print (marksz)

Note:

It should be noted that there are several potential applications for zip.

We have several relevant examples of how we will use such capabilities in a larger range. Examples include a student database, a scorecard, or any other utility that requires group mapping.

Scope @ NareshIT:

  1. At Naresh IT , you will find an experienced faculty that will lead, advise, and nourish you as you work toward your desired objective.
  2. Here, you will gain valuable hands-on experience in a realistic industry-oriented setting, which will undoubtedly help you design your future.
  3. During the application design process, we will inform you about other aspects of the application as well.
  4. Our expert trainer will explain the ins and outs of the problem scenario.

Our slogan is "achieve your dream goal." Our amazing team is working tirelessly to ensure that our pupils click on their targets. So, believe in us and our advise, and we promise you of your success.

FAQ's

1. What is the purpose of the zip() function in Python?

The zip() function in Python takes multiple iterables as input and returns an iterator of tuples. Each tuple contains elements from the corresponding position in each input iterable. This is often used to combine elements from different sequences into pairs or groups.

2. How do I use the zip() function with different-length iterables?

The zip() function will stop iterating when the shortest iterable is exhausted. If you want to continue iterating even after the shortest iterable is finished, you can use the itertools.zip_longest() function.

3. How can I unzip a sequence of tuples back into separate lists or iterables?

To unzip a sequence of tuples, you can use the zip() function in combination with the * operator. This unpacks the tuples into separate lists. For example:

Python

zipped_tuples = [(1, 2), (3, 4), (5, 6)]

list1, list2 = zip(*zipped_tuples)

print(list1) # Output: (1, 3, 5)

print(list2) # Output: (2, 4, 6)

For More Details Visit : Python Online Training

Register For Free Demo on UpComing Batches : https://nareshit.com/new-batches

要查看或添加评论,请登录

社区洞察

其他会员也浏览了