21
loading...
This website collects cookies to deliver better user experience
code for various iterators in python
comparing the time taken by the iterator to iterate
when to use which iterator
>>> names = ['ram','shyam','ajay','bipin','manoj','alex']
>>> for name in names:
... name = name.upper()
... print(name)
...
RAM
SHYAM
AJAY
BIPIN
MANOJ
ALEX
>>> names
['ram', 'shyam', 'ajay', 'bipin', 'manoj', 'alex']
>>> names
['ram', 'shyam', 'ajay', 'bipin', 'manoj', 'alex']
>>> reverse_iter = reversed(names)
>>> reverse_iter
<list_reverseiterator object at 0x000001D28CA21D20>
>>> for name in reverse_iter:
... print(name)
...
alex
manoj
bipin
ajay
shyam
ram
>>>
It uses a special function -
Suitable when we have to perfrom read/write operation on the iterable object.
Suitable for INPLACE list operations
It is slower compared to for iterator
>>> names = ['ram','shyam','ajay','bipin','manoj','alex']
>>> for i in range(len(names)):
... names[i] = names[i].upper()
... print(names[i])
...
RAM
SHYAM
AJAY
BIPIN
MANOJ
ALEX
>>> names
['RAM', 'SHYAM', 'AJAY', 'BIPIN', 'MANOJ', 'ALEX']
It uses a special function -
Suitable for Mutable iterable objects
Can be used for INPLACE Operations
Slow as compared to forward Iterator
>>> names = ['ram','shyam','ajay','bipin','manoj','alex']
>>> names_iter = enumerate(names)
>>> names_iter
<enumerate object at 0x000001D28CA413C0>
>>> for index,value in names_iter:
... names[index] = value.upper()
...
>>> names
['RAM', 'SHYAM', 'AJAY', 'BIPIN', 'MANOJ', 'ALEX']
It uses a special function -
Suitable when we have to iterate over mutile iterable object at the same time.
Return zip object
>>> first_names = ['ram','shyam','ajay','bipin','manoj','alex']
>>> last_names = ['gupta','tiwari','yadav','rawat','desai','khan','raven']
>>> home_town = ['ayodhya','vrindavan','bihar','jhnasi','boston','delhi','lanka']
>>>
>>> detail_iter = zip(first_names, last_names,home_town)
>>> detail_iter
<zip object at 0x000001D28CA41B00>
>>> for detail in detail_iter:
... print(detail)
...
('ram', 'gupta', 'ayodhya')
('shyam', 'tiwari', 'vrindavan')
('ajay', 'yadav', 'bihar')
('bipin', 'rawat', 'jhnasi')
('manoj', 'desai', 'boston')
('alex', 'khan', 'delhi')
It uses a special function -
It is used to iterate over the contents of the file
File Content :-
Details :-
('ram', 'gupta', 'ayodhya')
('shyam', 'tiwari', 'vrindavan')
('ajay', 'yadav', 'bihar')
('bipin', 'rawat', 'jhnasi')
('manoj', 'desai', 'boston')
('alex', 'khan', 'delhi')
Code :-
>>> for line in open("hello.txt"):
... print(line)
...
Output :-
Details :-
('ram', 'gupta', 'ayodhya')
('shyam', 'tiwari', 'vrindavan')
('ajay', 'yadav', 'bihar')
('bipin', 'rawat', 'jhnasi')
('manoj', 'desai', 'boston')
('alex', 'khan', 'delhi')
traverse on a TUPLE (readonly) - forward iterator
traverse on a LIST (readonly) - forward iterator
traverse modify list inplace - index based iterator
traverse mutiple iterable (readonly) - parallel iterator
Code :-
import time
lst = [i for i in range(10000000)]
print("Traversing over list of size 10000000 \n-----------------------------------------------------\n")
start_time = time.time()
for i in lst:
print("",end="")
end_time = time.time()
print(f"Time take by forward iterator {round(end_time - start_time ,2)} Seconds")
##################################################################
start_time = time.time()
for i in reversed(lst):
print("",end="")
end_time = time.time()
print(f"Time take by reverse iterator {round(end_time - start_time ,2)} Seconds")
##################################################################
start_time = time.time()
index_based_iter = range(len(lst))
for i in index_based_iter:
print("",end="")
end_time = time.time()
print(f"Time take by index based iterator {round(end_time - start_time ,2)} Seconds")
##################################################################
start_time = time.time()
index_based_iter = range(len(lst))
for i in index_based_iter:
print("",end="")
end_time = time.time()
print(f"Time take by enumerate iterator {round(end_time - start_time ,2)} Seconds")
Output :-
Traverseing over list of size 10000000
-----------------------------------------------------
Time take by forward iterator 6.99 Seconds
Time take by reverse iterator 7.45 Seconds
Time take by index based iterator 8.32 Seconds
Time take by enumerate iterator 7.15 Seconds