Ways to Iterate Through List in Python

Iteration Of A List In Python

In this tutorial, we’ll go over how to iterate through list in Python. Python List is basically an ordered data structure which enables us to store and manipulate the data in it.

Either of the following ways can be referred to iterate over a list in Python:

  • Using Python range() method
  • List Comprehension
  • Using Python enumerate() method
  • By using a for Loop
  • By using a while Loop
  • Using Python NumPy module
  • Using lambda function

1. Iterate through list in Python using range() method

Python’s range() method can be used in combination with a for loop to traverse and iterate over a list in Python.

The range() method basically returns a sequence of integers i.e. it builds/generates a sequence of integers from the provided start index up to the end index as specified in the argument list.

Syntax:

range (start, stop[, step])
  • start(upper limit): This parameter is used to provide the starting value/index for the sequence of integers to be generated.
  • stop(lower limit): This parameter is used to provide the end value/index for the sequence of integers to be generated.
  • step(optional): It provides the difference between each integer from the sequence to be generated.

The range() function generates the sequence of integers from the start value till the end/stop value, but it doesn’t include the end value in the sequence i.e. it doesn’t include the stop number/value in the resultant sequence.

Example:

lst = [10, 50, 75, 83, 98, 84, 32]

for x in range(len(lst)): 
	print(lst[x]) 

In the above snippet of code, the list is iterated using range() function which traverses through 0(zero) to the length of the list defined.

Output:

10
50
75
83
98
84
32

2. Iterate through list in Python using a for Loop

Python for loop can be used to iterate through the list directly.

Syntax:

for var_name in input_list_name:

Example:

lst = [10, 50, 75, 83, 98, 84, 32] 


for x in lst: 
	print(x) 

Output:

10
50
75
83
98
84
32

3. List Comprehension to iterate through a list in Python

Python List Comprehension is an indifferent way of generating a list of elements that possess a specific property or specification i.e. it can identify whether the input is a list, string, tuple, etc.

Syntax:

[expression/statement for item in input_list]

Example:

lst = [10, 50, 75, 83, 98, 84, 32] 

[print(x) for x in lst] 

Output:

10
50
75
83
98
84
32

4. Iterate through list in Python with a while loop

Python while loop can also be used to iterate the list in a similar fashion as that of for loops.

Syntax:

while(condition) :
	Statement
        update_expression
	

Example:

lst = [10, 50, 75, 83, 98, 84, 32]

x = 0

# Iterating using while loop 
while x < len(lst): 
	print(lst[x]) 
	x = x+1

Output:

10
50
75
83
98
84
32

5. Python NumPy to iterate through List in Python

Python NumPy Arrays can also be used to iterate a list efficiently.

Python numpy.arange() function creates a uniform sequence of integers.

Syntax for numpy.arange() function:

numpy.arange(start, stop, step)
  • start: This parameter is used to provide the starting value/index for the sequence of integers to be generated.
  • stop: This parameter is used to provide the end value/index for the sequence of integers to be generated.
  • step: It provides the difference between each integer from the sequence to be generated.

The numpy.nditer(numpy_array) is a function that provides us with an iterator to traverse through the NumPy array.

Example:

import numpy as np

n = np.arange(16)

 
for x in np.nditer(n): 
	print(x) 

In the above snippet of code, np.arange(16) creates a sequence of integers from 0 to 15.

Output:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

6. Python enumerate() method to iterate a Python list

Python enumerate() function can be used to iterate the list in an optimized manner.

The enumerate() function adds a counter to the list or any other iterable and returns it as an enumerate object by the function.

Thus, it reduces the overhead of keeping a count of the elements while the iteration operation.

Syntax:

enumerate(iterable, start_index)
  • start_index: It is the index of the element from which the counter has to be recorded for the iterating iterable.

Example:

lst = [10, 50, 75, 83, 98, 84, 32]

for x, res in enumerate(lst): 
	print (x,":",res) 

Output:

0 : 10
1 : 50
2 : 75
3 : 83
4 : 98
5 : 84
6 : 32

7. Iterating a Python list using lambda function

Python’s lambda functions are basically anonymous functions.

Syntax:

lambda parameters: expression
  • expression: The iterable which is to be evaluated.

The lambda function along with a Python map() function can be used to iterate a list easily.

Python map() method accepts a function as a parameter and returns a list.

The input function to the map() method gets called with every element of the iterable and it returns a new list with all the elements returned from the function, respectively.

Example:

lst = [10, 50, 75, 83, 98, 84, 32] 

res = list(map(lambda x:x, lst))

print(res) 

In the above snippet of code, lambda x:x function is provided as input to the map() function. The lambda x:x will accept every element of the iterable and return it.

The input_list (lst) is provided as the second argument to the map() function. So, the map() function will pass every element of lst to the lambda x:x function and return the elements.

Output:

[10, 50, 75, 83, 98, 84, 32]

Conclusion

In this article. we have unveiled the various techniques to iterate a Python List.


References

  • Iterate over a Python List – JournalDev