In this tutorial, well 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. Show
Either of the following ways can be referred to iterate over a list in Python:
1. Iterate through list in Python using range() methodPythons 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])
The range() function generates the sequence of integers from the start value till the end/stop value, but it doesnt include the end value in the sequence i.e. it doesnt 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 LoopPython 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 PythonPython 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 loopPython 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 PythonPython 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)
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 listPython 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)
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 functionPythons lambda functions are basically anonymous functions. Syntax: lambda parameters: expression
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]
ConclusionIn this article. we have unveiled the various techniques to iterate a Python List. References
|