Python Zip, enumerate function and frozenset() method

In this tutorial, you are going to learn about the zip function, enumerate function and frozenset () method in Python.

Table of contents:

  • Python Zip Function
  • Python enumerate function
  • frozenset() method in Python

Zip function in Python

The zip is a file extension which is used to store the files. In python, zip () function is used to store the data (i.e., list, tuple, dictionary, etc) which will iterate over each other one by one to give the output. The data can be of a different kind into the zip function i.e., one can be the tuple and other can be the list. The result of the zip function is always a tuple. The type of result can be changed with the help of the type conversion. You will understand more clearly the zip () function with the help of the program.

Program using zip() function:

  1. Both the data are of different type ( ‘c’ is a tuple and ‘e’ is a list).
  2. The for loop will iterate over the data one by one.
  3. Print the variable in the loop to get the output.
# tuple
c=('m','n')
# list
e=['f','h']

# use of zip()function
for i in zip(c,e):
    print(i)
# to get the type   
print(type(i))

Output:-

('m', 'f')
('n', 'h')
<class 'tuple'>

Program having different length of data:

If the lists are of different length then the output will be up to the common length of the lists.

# list 1
l=['a','s','d','b']
# list 2
c=['m','n','g']
# list 3
e=['f','h']
# Zip()function
for i in zip(l,c,e):
    print(i)

Output:-

('a', 'm', 'f')
('s', 'n', 'h')

Program having dictionary as the data:

# dictionary
l={1:'a',2:'s',3:'d',4:'b'}
# list 1
c=['m','n','g']
# list 2
e=['f','h']
# Zip()function
for i in zip(l.values(),l.keys(),c,e):
    print(i)

Output:-

('a', 1, 'm', 'f')
('s', 2, 'n', 'h')

Enumerate function in Python

Enumerate is a built-in function of the python. It helps us to get the index value with the data. The data can be anything like a list, tuple, set etc. Here is an example:

Program using enumerate function:

# list
sub=['Maths','Physics','Computer','Biology']
# use of enumerate function
for i,j in enumerate(sub):
    print(i,j)

Output:-

0 Maths
1 Physics
2 Computer
3 Biology

Program having index value according to the user input:

Enumerate function has a second parameter in which you can pass the index value which you want to give to the values of the tuple. By default the value is 0. In the program the index value starts from 11 as given by me as the second parameter in the enumerate function.

# tuple
sub=('Maths','Physics','Computer','Biology')
# use of zip()function
for i,j in enumerate(sub,11):
    print(i,j)

Output:-

11 Maths
12 Physics
13 Computer
14 Biology

Frozenset() method in Python

This method will create an immutable(i.e., non-changeable ) sets. You can pass the set, tuple or list in the frozen method.

Program using frozenset() method:

# frozenset() method
sub=frozenset({'Maths','Physics','Computer','Biology'})
print(sub)
# to get the type
print(type(sub))

Output:-

frozenset({'Biology', 'Computer', 'Maths', 'Physics'})
<class 'frozenset'>

Go and check other tutorials on python:

https://www.codespeedy.com/category/python/

Leave a Reply

Your email address will not be published. Required fields are marked *