zip function in python 3 tutorial

zip function in python 3 tutorial: The zip () function is defined as zip (*iterable) that takes in iterables as arguments and returns an iterator. The iterator will generate a series of tuples containing elements from each iterable. The zip () function can accept any type of iterable as files, lists, dictionaries, sets. The function will return a zip object, which is called iterator of tuples. The first item in each passed iterator is paired together, and the second item in each passed iterator is paired. The zip () function is used to make an iterator aggregates elements from each of the iterables.
The iterator will stop when the shortest input iterable is exhausted. The single iterable argument returns an iterator of 1-tuples with no arguments and returns an empty iterator. If the iterators have different lengths the iterator with the least items decides the length of the new iterator.

Syntax:-

zip (iterator1, iterator2, iterator3 ...)
Parameter Values:-


parameters

description

Iterator 1, iterator 2, iterator3...

The iterator object will be joined together

Example:-

A= (“John”,”charles”,”Mike”)
B= (“Jenny”,”Christy”,”Monica”,”vicky”)
X=zip (a, b)
Here the function will take iterables and aggregate them in a tuple that returns.
Here the purpose is map similar index of multiple containers and uses a single entity.

Return value from zip ():-

Here the zip () function will return an iterator of tuples which are based on the iterable objects.
If we do not pass parameter the zip will return an empty iterator.
Then after passing the single iterable the zip () will return an iterator of tuple with each tuple having one element.
If multiple iterables are passed then zip () will return an iterator of tuples with each tuple having elements from all iterables.

Example:-

Number_list= [1, 2, 3]
Str_list= [‘one’,’two’,’three’]
Result=zip ()
Result_list=list (result)
Print(result_list)
Result=zip (number_list, str_list)
Result_set=set (result)

Output:-
[]
{(2,’two’, (3,’three’), (1,’one’)}

Unzipping Values in Python:-


Using the zip function as,


z=zip ([1, 2, 3], ['a','b','c'], ['#','*','$'])

a, b, c=zip(*z)

a,b,c

((1, 2, 3), (‘a’, ‘b’, ‘c’), (‘#’, ‘*’, ‘$’))


z=zip ([1, 2], [3, 4, 5])

a, b=zip (*z)

a, b

((1, 2), (3, 4))

Zip () used for loop through multiple lists:-

Example:-
Name= [‘sam’,’susma’, kavita’’]
Age= [25, 50, 20]
For x, y in zip (name, age)
Print(x, y)
Output:-

# sam 25
# Susma 50
# kavita 20 

Iterables with Different Length:-

If we pass iterables having different length the iterable with least items will decide the length of the iterable.
Example:-

x = [1, 2, 3, 4]
y = ['one', 'two']
z = ['I', 'II', 'III'] 
result = zip(x, y, z)
print(result)
Output:-
[(1,’one’,’I’), (2,’two’,’II’)]
 

Compare Zip () Python 2 vs. 3:-

The zip () function has got a change in the behavior in Python 3.
 It used to return a list of tuples of the size equal to short input iterables as an empty zip () call would get you an empty list in python 2.
Here in Python 3, the zip () is reimplemented to return an iterator which depends upon traversing.
 It will get tuple from the zipped result one by one and you can iterate over it only once.

Applications:-

Many applications can be executed using zip function as is it student database or scorecard or any other utility that requires mapping of groups.

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India