Using Iterations in Python Effectively


In this article, we will learn about how to implement iterators and their effective implementation in Python 3.x. Or earlier. Let’ s take a look at various methods available in python which implements iterators.

Type 1 − Implementation Of While Loop With Known Length

Example Code

 Live Demo

genre = ("Python","C","C++","Java")
print("The topic available on Tutorial's Point are:")
i = 0
while (i < len(genre)):
   print (genre[i])
   i += 1

Explanation

Due to its less compact structure, this method is not favored. Error Handling is also difficult in this case . Large-scale programs or designs doesn’t use this approach.

Output

The topic available on Tutorial's Point are:
Python
C
C++
Java

Type 2 − Implementation of for loop with known length

Example Code

 Live Demo

genre = ("Python","C","C++","Java")
print("The topic available on Tutorial's Point are:")
for i in range(len(genre)):
   print (genre[i])

Explanation

This is the most favoured method used by programmers. Here range() functions serves as a medium to increment or decrement the iterator. By default, it increments the iterator by 1. In case we specify the step of incrementation or decrementation, it works according to the step value provided.

Output

The topic available on Tutorial's Point are:
Python
C
C++
Java

Type 3 − Implementation Of for loop without length

Example Code

 Live Demo

genre = ("Python","C","C++","Java")
print("The topic available on Tutorial's Point are:")
for i in genre:
   print (i)

Explanation

This method is usually favoured in linear data structures like lists, dictionary, tuples, n dimensional-arrays, etc. The iterator traversed each component of the specified structure and displays data to the console. Incrementation is automatic in this type.

Output

The topic available on Tutorial's Point are: Python C C++ Java

Type 4 − Implementation Via Enumerate Data Type

Example Code

 Live Demo

genre = ("Python","C","C++","Java")
iterator = enumerate(genre)
print("The topic available on Tutorial's Point are:")
for i,v in iterator:
   print (v,end="\t")

Explanation

In this case enumerate helps us to create a dictionary in which indexes serves as keys and values in the list as their corresponding values. Here we have to specify two iterators; one for the index and another for the value to be displayed.

Output

The topic available on Tutorial's Point are:
PythonCC++Java

Type 5 − Implementation Via Zip Function

Example Code

 Live Demo

genre = ("Python","C","C++","Java")
extras = ["C#","Dart","Erlang","Go"]
print("The topic available on Tutorial's Point are:")
for i, j in zip(genre,extras):
   print (i, j,sep="\t")

Explanation

Here we specify two linear data structure i.e. list, array or tuple with the help two iterators. To do this we take the help of zip function which comes very handy while handling various scenarios. It takes up the data structure with a shorter length and skips the content of the larger data structure.

Output

The topic available on Tutorial's Point are:
PythonC#
CDart
C++Erlang
JavaGo

Conclusion

In this article, we learnt how to implement the Stack & Queue data structure in Python 3.x. Or earlier. You can implement the same algorithm to implement a stack/queue detector program in any other programming language.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements