Iterator Data-Types in Python Programming Language

Iterator data type in python

Iterator in Python

Iterator Data-types in Python programming language is also a method. Iterator in While loop is a variable defined before we initialize the loop. While the loop is used to perform operations or task where the ending is not properly defined. In while loop iterator value is manual initialized and incremented. While loop iterates through the elements of the sequence and operations are performed.

Iterator Data-Type

As we discussed above, Iterator Data Type in Python is a method. There are two types of Iterator data types in Python:

  1. __iter__
  2. __next__

We can prefer below image for a brief discussion of these two Iterator data type. 

Iterator data-type in Python programming language

Methods of Iterator data-type

__iter__

Iterator is a method in python programming used to iterate through the elements of a sequence. iterator function comes with a function called as __iter__() function to iterate through the sequence. The iter() function takes the argument for the iterable data-type and creates it an iterable object. iter() function is used in replacement to loops. using the iter() function and using another function of iteration method we can parse the nest element

Run
#iter function using while loop
#initialize a variable
variable = '12345'
#pass the variable to iter function
iter_value = iter(variable) 
while True: 
    try: 
        #iterating to the next elements of iterable
        element = next(iter_value) 
        print(element,end=' ') 
    #if it reaches the limit of length of an iterator.
    except StopIteration: 
    break
print()

#iter function using while loop
#initialize a variable
variable = 'PrepInsta'
#pass the variable to iter function
iter_values = iter(variable)
for i in range(len(variable)):
    #iterating to the next elements of iterable
element = next(iter_values)
print(element,end=' ')
Output:
1 2 3 4 5 
P r e p I n s t a

__next__

iter() function creates the object as an iterable object, but how to get the values from the sequence? Here in the frame comes the nest function of iteration method called as __next__() function. next() function is used to iterate through the elements os a sequence and checks for the next available element. If no element is found in the sequence than an exception “StopIterationError”. to parse at the next element of the sequence we use the next() method.

Run
#initialize an list 
arr = [1410, 'DJ', 19.97]

#pass the list to iter function
next_arr = iter(arr)

#using next function iterate over the next element
element = next(next_arr)
print(element)

element = next(next_arr)
print(element)

element = next(next_arr)
print(element)

#check for more result
try:
element = next(next_arr)
print(element)
#handle the exception
except StopIteration:
print('No more elements')

#next function using while loop
#initialize a variable
variable = '12345'
#pass the variable to iter function
iter_value = iter(variable)
while True:
    try:
        #iterating to the next elements of iterable
        element = next(iter_value)
        print(element,end=' ')
    #if it reaches the limit of length of an iterator.
    exceptan StopIteration:
        break
print()
Output:
1410
DJ
19.97
No more elements
1 2 3 4 5

Iterator in different Control Statements

Iterator in for Loop

for Loop is used when we want to repeat a task in the loop and ending pointy is known. Using for loop we can perform many operations like if we want to print elements of a list or perform some task on elements of a list, etc. Iterator is the variable responsible for working of for loop. iterator iterates through the element and performs operations we want to.

Run
#print iterators upto a range
#take user input of range
val = int(input('Enter the ranger :'))
for i in range(val):
    print(i,end=' ')
print()

#iterating through list data-type
lis = [1,2,3,4,5,6]
for i in range(len(lis)):
    print(lis[i],end=' ')
print()

#iterating through string data-type
string = 'PrepInsta'
for i in string:
   print(i,end=' ')
print()

#iterating through dictionary
dictio = {1:'Divyanshu', 2:'875597', 3:'CS'}
for i in dictio.keys():
    print(i,dictio[i])
Output
Enter the ranger :5
0 1 2 3 4 
1 2 3 4 5 6 
P r e p I n s t a 
1 Divyanshu
2 875597
3 CS

Iterator in while Loop

Iterator in while Loop is a variable defined before we initialize the loop. While the loop is used to perform operations or task where the ending is not properly defined. In while loop iterator value is manual initialized and incremented. While loop iterates through the elements of the sequence and operations are performed.

Run
#print iterators upto a range
#take user input of range
val = int(input('Enter the ranger :'))
i = 0
while i < val:
    print(i)
    i+=1
#iterating throug list data-type
lis = [1,2,3,4,5,6]
i = 0
while i < len(lis)
    print(lis[i],end=' ')
    i==1
print()

#itaratring through string data-type
string = 'PrepInsta'
i = 0
while i < len(string):
    print(string[i],end=' ')
    i+=1
print()
Output:
Enter the ranger :5
0 1 2 3 4 
1 2 3 4 5 6 
P r e p I n s t a

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription