The List type is used to store multiple items in a single variable. The list can store item types like integer, string, dictionaries even another list. Python List type is an iterable type that can be iterated by using different ways. In this tutorial, we will learn how to iterate through a Python list in different ways.
Iterate Through List with for Loop
The for loop is the defacto way to iterate through a list. For loop is used to iterate for the given iterable data structure like list, tuple, dictionary, set, etc. The for loop has the following syntax.
for VAR in LIST: print(VAR)
Now, look at the following list example where we will iterate over the list named numbers and print them to the standard output with the print() method. In every iteration, one item will be set into the variable named item.
numbers = [1,2,3,4,5] for item in numbers: print(item)
Iterate Through List with for Loop and range()
Python provides the for loop in a modern way without extra iteration statements which exist in traditional languages like C, C++, etc. But if we need we can use the range() method with the for loop to iterate more traditional way by increasing the index with the for loop and returning the list item with an index number.
LENGTH = len(LIST) for INDEX in range(LENGTH): print(LIST[INDEX])
In the following example, we will create a number list of names numbers and then get the item count with the len() method. The item count is set as the length for range() method and iterated from 0 to 4 because the numbers list has 5 items.
numbers = [1,2,3,4,5] length = len(numbers) for index in range(length): print(numbers[index])
Iterate Through List with while Loop
Even though the for loop is very popular the while loop provides similar features that we can also use to iterate through the list. We will use the len() method to get the item count for the list and then iterate and increase the index to the end of the list.
LENGTH = len(LIST) while INDEX < LENGTH: print(LIST[INDEX]) IDENX +=1
Let’s make the example about how to iterate through a list with a while loop.
numbers = [1,2,3,4,5] length = len(numbers) while index < length: print(numbers[index]) index +=1
Iterate Through List with Comprehension
Python list provides different and useful features which makes it very powerful. List comprehension is a feature that is used to iterate over the list items. The syntax for the list comprehension is like below.
[print(ITEM) for ITEM in LIST]
In the following example, we will iterate through the list by using list comprehension.
numbers = [1,2,3,4,5] [print(item) for item in numbers]
Iterate Through List with enumerate() Method
Tuples are another iterable type in Python and the enumerate() method can be used to convert a list into a tuple which can be iterable too. While converting the index number will be also returned to in the tuple.
for INDEX,ITEM in enumerate(LIST): print("Index:",INDEX,"Item:",ITEM)
In the following example, we will iterate over the list named numbers and the enumerate() method which will return items and index numbers like a tuple. We will set the index as index variable and item as item variable.
numbers = [1,2,3,4,5] for index,item in enumerate(numbers): print("Index:",index,"Item:",item)
The index starts from 0 and incremented 1 by 1.