Join my course at Udemy (Python Programming Bible-From beginner to advanced )

Blogger templates

Saturday 23 May 2020

range, zip and enumerate



The traditional looping technique of python is For and While loop but Python also provides more specialized looping techniques in the form of built-in function. Note that the specialized functionality can also be achieved using For and While loop but the built-in function in cleaner and neater way.

  • Range - creates sequence of numbers that can be used with any another python statement.
  • Zip - Zip function combines multiple object element by element and returns the combined or zipped version of new object in the tuple format.
  • Enumerate - It generates both values and indexes of the item and returns both of them in a tuple format.
  • map  - map allows to apply a function to each element of the container sequence object and the result can be collected as the return value of the function.

In this post we will go through range, zip and enumerate built in function. We will study map object in the later post.

range 

range built-in function allows to generate numbers based on start, end and skip values provided.

list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(2,10))
[2, 3, 4, 5, 6, 7, 8, 9]
list(range(2,10,2))
[2, 4, 6, 8]

range built-in function works with for loop to iterate through sequence containers based on the numbers created via range function that can be used as index to the sequence containers. Following example shows multiple ways to traverse string.

##Print String
S="HelloPython"
for i in range(0, len(S)):
    print(S[i], end=",")
H,e,l,l,o,P,y,t,h,o,n,
##Skip Print String
S="HelloPython"
for i in range(0, len(S),2):
    print(S[i], end=",")
H,l,o,y,h,n,

zip

zip allows to use multiple sequence in parallel and it returns the combined or zipped version of element in the tuple format.

##Zipping two list.
L1 = [1,2,3,4,5,6,7]
L2 = [8,9,10,11,12,13,14]
ZipList = zip(L1,L2)
print(list(ZipList))
[(1, 8), (2, 9), (3, 10), (4, 11), (5, 12), (6, 13), (7, 14)]


zip can be also be used in conjunction with for loop to loop through combined version of sequence object element by element.

##Zipping two list with FOR loop.
L1 = [1,2,3,4,5,6,7]
L2 = [8,9,10,11,12,13,14]
ZipList = zip(L1,L2)
for (x,y) in ZipList:
    print(x,y,sep=",", end="\n")
1,8
2,9
3,10
4,11
5,12
6,13
7,14

if there is size mismatch then zip takes the sequence element with lowest size and then combines the element and skips the elements that can't be combined.


##Zip with more than two lists.
L1 = [1,2,3,4,5,6,7]
L2 = [8,9,10,11,12,13,14]
L3=[15,16,17,18,19,20,21]
L4=[22,23,24,25,26,27,28]
ZipList = zip(L1,L2,L3,L4)
for (x,y,z,k) in ZipList:
    print(x,y,z,k,sep=",", end="\n")
1,8,15,22
2,9,16,23
3,10,17,24
4,11,18,25
5,12,19,26
6,13,20,27
7,14,21,28


zip can also be used to create a dictionary by passing keys and values in separate list.

##Joining two strings  with Mismatch in length
S1="abcde"
S2="fghijkl"
zipS = zip(S1,S2)
for (x,y) in zipS:
    print (x,y,sep=",", end="\n")
a,f
b,g
c,h
d,i
e,j

enumerate

 It returns offset and indexes of the sequence objects. enumerate creates a generator object so that it can be used in conjunction for loop. 

S = "Hello"
for (index, item) in enumerate(S):
    print(index, item, sep = ",", end ="\n")
0,H
1,e
2,l
3,l
4,o 

It can also be used to traverse a file line by line using manual loop with next function or in a automatic loop with For. This is possible because enumerate returns generator object that provides automatic way to traverse sequence object item by item.

S="HelloPython"
x=enumerate(S)
print(x)
print(next(x))
print(next(x))
<enumerate object at 0x0000023CA27D0C18>
(0, 'H')
(1, 'e')

Conclusion

In this post we have seen range, zip and enumerate to traverse through python container objects and performed specified operation. In the next post we will go through iteration and comprehension that provides another approach to traverse a python container objects.
Share:

0 comments:

Post a Comment

Feature Top (Full Width)

Pageviews

Search This Blog