Python for in, while & enumerate loop with list index

Not familiar with python for in, while and enumerate loop and how to get the list index of elements?  Here is a descriptive guide to getting you started.

Looping in python is a little bit confusing because the for loop in python is not for loop, it is actually a for each loop.

If you are familiar with loops in any other programming languages like c,c++, and java then you might be a little bit confuse with for loop in python.

Now in this article, we will be discussing for loops in python and we will compare it with for loop in other programming languages.

Now have a look at for loop in javascript.

Code :

var cities = [‘’central_city”,”gautham_city”,”new_york”];
for (var i = 0;i<cities.length;i++)

Output :

  central_city              gautham_city              new_york

In the above code, we set the variable i = 0 .

Then we check if the value of i is less than the length of the array.

If yes then print the value of array named cities if no then exit from the loop.

This is the normal looping syntax that is same for the traditional programming languages like c,c++ and java.

Now let’s talk about looping especially for in python index.

First of all, we should see the more convenient looping method in python.

Suggested read How to do String comparison in python

Python while loop:

 Code :

Cities =  [‘’central_city”,”gautham_city”,”new_york”]
i = 0
while i<len(cities):
   i += 1


 central_city              gautham_city              new_york

This while loop performs and works just like for loops in other programming languages.

Also check :   Python string to int conversion tutorial with examples

Initially the variable i contains the value 0 and the print statement run while the value of i is less the length of cities array.

This looping method in python is very uncommon.

We often see python programmers loop through anything in a new fashion or style and they don’t use while loop in general.

 Example code: 

Cities =  [‘’central_city”,”gautham_city”,”new_york”]

for i in range(len(cities))

Output :

central_city              gautham_city              new_york

Explanation :

According to indexes of our array of cities, this method first decides the range of the variable i from our list of cities and then perform print() operation accordingly.

This method also provides us the indexes of each item that are present in our list or array of cities as the for loop provides in other programming languages like c,c++, and java.

Suggested read Check if a Python string contains another string

Python For in loop

Both loops in python, while loop and range of len loop perform looping operations over the indexes.

But most of the cases we don’t need to know about the indexes.we are only using these indexes for retrieving the data from our array.

So now we do not actually care about indexes, there is a very simple way in python for looping the for-in loop.

Example code :

cities =  [‘’central_city”,”gautham_city”,”new_york”]

for city in cities :

Output :

 central_city              gautham_city              new_york

In this loop, we don’t get the index of every element.

Now, what happens when we actually need the index of the elements of the array.

Also check :   Determine length of list python : Tutorial with examples

Then we can use the enumerate built-in function in python with the help of this function we can retrieve the elements of the array and corresponding indexes as well.

Suggested read What are lambda functions in python?

Python Enumerate

Cities =  [‘’central_city”,”gautham_city”,”new_york”]

for position , name in enumerate(cities,start = 1):
     print (“city {}: {}”.format(position, name))

This enumerate function gives us the index and the element of the array.

So when we want to perform looping in an array and we want the indexes of every element present in the array then we should go with the enumerate function.

Conclusion :

We saw almost every type of basic looping method in python that is widely used.

And we compared the looping method of python with the looping method of other programming languages like c,c++, java, and javascript.

As we saw looping in python is a little bit confusing if you are coming from c,c++and javascript background.

But once you get familiar with the looping methods in python then it seems to be very easy for you.

Practice make programming easy there is no shortcut for will not become a good programmer if you only know the theoretical part of languages.

After learning the basic concepts of any programming language start making projects on the language or relevant programs.

Keep learning, stay updated, and don’t learn only the concept of programming language try to apply it in the real world.

Let us know if we missed anything while compiling this guide on loops – for in python index.

Also check :   How to read a text file line by line in python explained with examples

Get occasional tutorials & guides in your inbox. No spam ever.

Buy Me A Coffee

Leave a Comment