The list data structure is utilized to store collections of data containing different data types. There are a number of operations that are performed on the list such as concatenation, replacement, removal of the element, etc.
Iterating over the list performs some operations on the list. So Python provides various methods to iterate over a list. In this post, we will explain the below-listed methods to iterate through the list using relevant examples:
- Method 1: Using for Loop
- Method 2: Using Loop and range() Function
- Method 3: Using While Loop
- Method 4: Using List Comprehension
- Method 5: Using enumerate() Function
- Method 6: Using lambda Function
Method 1: Using for Loop
The below code uses the traditional “for loop” to iterate over the list:
Code:
list_value = [1, 5, 7, 9, 11]
for i in list_value:
print(i)
The “for loop” iterates over the list named “list_value” and displays each element using print() function.
Output:
The elements of the list have been successfully iterated.
Method 2: Using loop and range() Function
In the following code, the “for loop” is used along with the “range()” and “len()” functions to iterate over the list:
Code:
list_value = [45, 25, 10, 30]
for item in range(len(list_value)):
print(list_value[item])
- The “len()” function returns the length of the given list.
- The length of the list value is passed as a parameter to the “range()” function to generate the range.
- The for loop iterates through the range and the “list_value[item]” returns the element of the list.
Output:
The input list has been iterated.
Method 3: Using While Loop
The following code uses the “while loop” to iterate over the input list:
Code:
list_value = [5, 10, 25, 30, 35]
num = 0
while num < len(list_value):
print(list_value[num])
num = num+1
- The list is initialized and the “0” is assigned to a counter variable named “num”.
- The while loop is used to check the condition that the counter number is less than the length of the initialized list.
- If yes, then the element value is printed on the screen for that specific index.
- The while loop will iterate through the list until the specified condition remains true, and print all elements on the screen.
Output:
The initialized list has been iterated successfully.
Method 4: Using List Comprehension
The list comprehension technique is used in the below code to iterate over the list:
Code:
list_value = [1, 5, 7, 9, 11]
[print(i) for i in list_value]
The “for loop” and “print()” function is used in the list comprehension to iterate over the list.
Output:
Each list element has been iterated.
Method 5: Using enumerate() Function
The “enumerate()” function is used in the below code to iterate through the list:
Code:
list_value = [25, 10, 15, 20]
for index, value in enumerate(list_value):
print (index, value)
- The “enumerate()” function accepts the list as an argument and returns the enumerated object.
- For each index value, the for loop iterates over the enumerated object and shows the index of the list along with the value.
Output:
The given list has been iterated.
Method 6: Using lambda Function
In the following code, the lambda function is used to iterate through the list:
Code:
list_value = ['A', 'B', 'C', 'D']
print(list(map(lambda x:x, list_value)))
- The “map()” function is used to apply the lambda function to each element of “list_value”.
- The resultant map object is then converted into a list utilizing the “list()” function.
Output:
The list has been iterated successfully.
Conclusion
To iterate through the list, the for loop, list comprehension, loop with range() function, While loop, enumerate(), and lambda functions are used in Python. The traditional “for loop” is a simple and efficient way to iterate over the list. We can use list comprehension, while loop, and range() functions along with for loop to iterate over the given list. This article presented various ways to iterate through the list in Python.