How to Iterate a List in Python
As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and many more.
What is a List in Python?
List can be defined as the ordered collection of mutable elements, which can be of any data type declared inside the [ ] separated by commas after each element. Lists are indexed, so the elements inside the List can be accessed anytime through indexing the List.
Create a List in Python
As discussed above, Lists can be declared inside the square brackets, separated by commas and can be of any data structure.
Code
Languages_List = ['Python' , 'C++' , 'Java' , 'C#']
print(Languages_List)
Output
['Python','C++','Java','C#']
Accessing Elements through Indexing from the List
As discussed earlier, the elements we declare inside the List have indexes to access the elements, modify if necessary, and many other functions. We will now try to access the elements inside the Languages_List declared above through indexing.
See the below code for accessing the elements inside the List
Code
Languages_List = ['Python', 'C++' , 'Java' , 'C#']
print(Languages_List[1])
print(Languages_List[3])
Output
['C++']
['C#']
Now that we have seen what is a List, creating a List, and accessing the elements inside the List, we will now closely look at iterating through the Lists.
There are various methods to iterate through a List in Python, some of them being using the built-in functions in Python, iterating through the loops, namely by using a for loop or a while loop, range function, using the method of List comprehension, with the help of enumerate function.
Method-1: Using the for loop to Iterate through a List in Python
Code
List_nums = [21,33,567,986,999]
for i in List_nums:
print(i)
Output
21
33
567
986
999
In the above code, we see that a List is declared in which it has all the numbers as its elements declared inside the square brackets separated by commas. Below the creation of the List, we have written the code to iterate through the elements inside the List using a for loop where i is the iterator. When the iterator is printed using the print function, all the elements inside the List are printed as output.
Method-2: By using the range function along with for loop
Code
List_nums = [21,33,567,986,999]
Length = len(List_nums)
for i in range(Length):
print(List_nums[i])
Output
21
33
567
986
999
In the above code, we see that along with the variable containing list elements; we have declared another variable named length, which is calculating the length of the List that is declared, and while running the for loop over the List, we have used the range function in which the length variable which calculated the length of the List for us is passed as the argument. When the iterator is printed, we see that all the elements inside the List are printed as output.
Method-3: Using the while loop to Iterate through a List in Python
Code
List_nums = [21,33,567,986,999]
Length = len(List_nums)
i=0
while i<Length:
print(List_nums[i])
i+=1
Output
21
33
567
986
999
As we see, we have accessed the elements inside the List containing numbers using a while without losing the generality of the while loop syntax and with the help of another variable-Length which calculated the length of the List. Here the iterator i is declared outside the while condition and incremented after the print function is written.
Method-4: Using the List Comprehension Method in Python
Code
List_nums = [21,33,567,986,999]
[print(i) for i in List_nums]
Output
21
33
567
986
999
In the above code, we have used the concept of List Comprehension, which is familiarly known as the short method, to use the loop concept to iterate on short looping. Here the print function is passed with a parameter of i with the for loop iterating on the List, and hence we see the elements are printed.
Method-5: Using the Built-in enumerate function in Python
Code
List_nums = [21,33,567,986,999]
for i, val in enumerate(List_nums):
print(i,",",val)
Output
0, 21
1, 33
2, 567
3, 986
4, 999
As we see, the enumerate function is passed along with the val variable, which calculates the index of the elements inside the List printed.