A python list is a collection of elements of same or different types.
Iterating over a list is the most required operation for many tasks such as addition of its elements, finding the largest element in the list etc.
This article will explain different ways in which a list can be iterated in python.

Method 1: Using for loop with index

A python list is an indexed collection meaning all its elements have numeric indexes starting with 0.
A list can be iterated using for loop starting with 0 till the length of the list using range function.
for loop with range function iterates from 0 till the number provided to the range function as argument.
Example,

# create a list
strings = ["Python","fun", "is", "learning"]
# calculate length of list
total_items = len(strings)
# iterate using for loop
for count in range(total_items):
   print(strings[count])

Above example prints

Python
fun
is
learning

Method 2: Using for loop and in operator

for loop along with in operator is a simplest way to iterate a list. Syntax is

for <variable> in <list>:

Here, variable holds the next list item in every iteration starting with first.
Example,

# create a list
strings = ["Python","fun", "is", "learning"]
# iterate using for in loop
for word in strings:
   print(word)

Output of above example is

Python
fun
is
learning

Method 3: Using while loop

This method works in the same way as the indexed for loop in that the elements are accessed using index but it uses a while instead of for.
Example,

# create a list
strings = ["Python","fun", "is", "learning"]
# calculate length of list
total_items = len(strings)
# initialize loop counter
count = 0
# iterate using while loop
while count < total_items:
   print(strings[count])
   # increment counter
   count += 1

Output is the same as above methods.

Method 4: Using enumerate()

Python’s built-in function enumerate accepts an iterable(such as list) as argument and returns a list of tuples where each list element is a tuple whose first element is a count and second is the list item itself.

Syntax of enumerate is

enumerate(iterable, start=0)

where first argument is the iterable and second is a counter.
Counter is optional and defaults to 0 if the second argument is not provided.
Example,

#list of string
strings = ["Python","fun", "is", "learning"]
# assign the return value from enumerate to variable
enumerateType = enumerate(strings)
# convert to list
enumerate_list = list(enumerateType)
# print its value
print(enumerate_list)
# iterate the list
for i, word in enumerate_list:
   print(i,word)

Output of above code is

[(0, ‘Python’), (1, ‘fun’), (2, ‘is’), (3, ‘learning’)]
0 Python
1 fun
2 is
3 learning

Look, the return value of enumerate() when converted to a list is a list of tuples and how it is iterated.
It is also possible to modify the first value of tuples to start with some other value by providing it as the second argument to enumerate.
Example,

enumerateType = enumerate(strings, start = 10)

With start value set to 10, the list of tuples will change to

[(10, ‘Python’), (11, ‘fun’), (12, ‘is’), (13, ‘learning’)]

Conclusion

In conclusion, this article has explored four distinct methods for looping or iterating through a list in Python, each offering its own advantages and use cases.

Method 1 demonstrated how to use a for loop with index to access each element in the list sequentially, providing control over both the index and the element.
Method 2 showcased the simplicity of using a for loop with the in operator to iterate through the list directly, offering a concise and intuitive approach.
Method 3 introduced the utilization of a while loop for iteration, providing flexibility for more complex looping conditions.
Lastly, Method 4 highlighted the versatility of the enumerate() function for looping, allowing simultaneous access to both the index and the element in a single iteration.

By mastering these four methods, Python developers can efficiently traverse lists in various scenarios, enhancing code readability and efficiency in their projects.

Leave a Reply