What is Zip and UnZip Function in Python - NareshIT
Naresh i Technologies
Only Institute to offer the 'Most Comprehensive eLearning Platform to suit the self-learning needs of all CMS and LMS
Introduction:
Zip Function in Python:
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:
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