How to Loop Through a List in Python

The list data structure in Python helps us store the collection of any data type in a single variable. It is mutable in nature which implies that the elements in the Lists can be changed/modified. The square brackets are used to declare a list in Python and the elements are stored in it using a comma-separated syntax. In certain scenarios, there is a need to iterate over the elements in the List, which can be achieved in Python using various methods. Looping effectively executes the code block repeatedly in Python. 

This write-up will explain the working of various methods to iterate/loop through the Python list.

How to Loop Through a List in Python?

Looping is an effective way to execute the same code multiple times without explicitly declaring it again and again. To loop through the List elements in Python different methods are used that iterate over until the specified condition is fulfilled.

The popular ways to loop through a Python List are listed below:

  1. Method 1: Iterate a List Using the for Loop
  2. Method 2: Iterate a List Using while Loop
  3. Method 3: Iterate a List Using List Comprehension
  4. Method 4: Iterate a List Using Enumerate
  5. Method 5: Iterate a List Using loop and range()
  6. Method 6: Iterate a List Using map() Function
  7. Method 7: Iterate a List Using Lambda Expression 

Let’s get started with the most convenient and classic one which is traditional for loop.

Method 1: Iterate a List Using the for Loop

The basic method to iterate over a Python list is the standard “for loop”. The code below implements the for loop to iterate each list element and print it on the console.

# Loop through using for loop
mylist = [1,2, 3, 5, 6, 7, 9, 15]
print("The List of Numbers are: ")
# Python for loop
for x in mylist:
    print(x)

In the above block of code:

  • A list is declared as “mylist” with integer values.
  • Next, the for loop is executed to iterate over the given list.
  • The print function of Python prints the list of integers.

Output

The output below indicates that the numbers are successfully printed using a for loop.

Method 2: Iterate a List Using while Loop

The while loop can also be utilized to iterate over the elements of a List effectively. Here is a practical illustration of how a list can be iterated using a while loop in Python.

# Loop through using while loop
mylist = [1, 2, 3, 5, 6, 7, 9, 15]
print("The List of Numbers using while() loop are: ")
x = 0
# Python while() loop
while x < len(mylist):
    # print() function to print the list
    print(mylist[x])
    x = x + 1

In the above code, 

  • A new list of integers is declared and stored in a variable named mylist.
  • Next, a variable “x” is initialized as 0. 
  • After that, the while loop is implemented to loop through the elements in the list.

Output

Each element of the mylist is successfully iterated and printed using the while loop:

Method 3: Loop Through a List Using List Comprehension

The list comprehension offers the simplest and the shortest syntax to loop through the list elements in Python. Here is how you can utilize the list comprehension to loop through the list elements:

#The integer values in the list
mylist = [1, 2, 3, 4, 5]
print("The items in the List are:")

# Using list comprehension
[print(x) for x in mylist]

In the above code, 

  • A list named “mylist” is initialized with integer elements.
  • The list comprehension method is applied to print each element of the given list.

Output

Each list element is iterated and printed successfully on the console using List comprehension:

Method 4: Iterate a List Using Enumerate

The enumerate() function can be executed to iterate over the list while keeping track/record of the index of each element.

# Loop through using enumerate()
mylist=["Hello", "in", "Python"]

# Using enumerate()
for x,value in enumerate(mylist):
print(x,":",value)

In the above code, 

  • A list of strings is declared and stored in “mylist”.
  • The enumerate() function is utilized to iterate over the Python list.

Output

The output below prints each list element along with respective indexes.

Method 5: Iterate a List Using Loop and range()

The range() function can be executed with the for loop to loop through the list elements. Here is how you can iterate over a Python list using a loop and the range() function:

mylist=["Hello", "in", "Python"]
listlen=len(mylist)
print("The length of the list is:",listlen)
for x in range (listlen):
print(mylist[x])

In the code above:

  • The Strings are declared in the List.
  • The next step involves calculating and printing the length of the list using the len() and print() functions, respectively.
  • The for loop and the range() function are used to iterate over the list within the specified range.

Output

The output below verifies that the given list is successfully iterated using the range() function.

Method 6: Loop Through a List Using map() Function

In Python, the map() function maps a given function to every single element of the iterable (like a list) and retrieves an iterable of the results. Therefore, it can be utilized to iterate over a List in Python.

def Method6(ele):
print(ele)
mylist = [11,12,13,15,18]
output=map(Method6, mylist)
for _ in output:
pass

In the code above:

  • The function is defined as “Method6” to loop through the elements.
  • The integers are declared in the list.
  • The map() functions take two arguments: the function that is “Method6” and the list that is “mylist”. 
  • The “output” of the map() function is utilized to iterate over the list elements.

Output

The output below shows that the elements of the list are successfully iterated and printed on the console.

Method 7: Loop Through a List Using Lambda Expression 

The lambda expression is another effective alternative that helps us iterate over a Python list. The lambda function/expression works similar to the user-defined functions. It can accept multiple arguments, perform the desired functionality, and retrieve only one result/expression. 

The below code loops through the list in such a manner that it uses a for loop to iterate over the elements in the list with lambda expression.

list1=[2,4,5,10]
list2=[]
#Lambda function to print the elements in the list
x = lambda i:i**2
for y in list1:
    # Append in list 2
    list2.append(x(y))
#Print the appended list
print("The Square of the numbers are:",list2)

In the code above, 

  • A couple of lists named “list1” and “list2” are declared.
  • List1 is initialized with integer values and list2 is initialized as empty.
  • The lambda expression is used to loop through each element of the list and calculate the square of each list element.
  • The append() function is utilized to insert the square of each element into the list2.
  • Finally, the list2 is printed using the print() function to verify the list iteration.

Output

The output confirms that the lambda expression successfully iterated over the given list using the for loop:

This is how a list can be iterated in Python using different methods.

Conclusion

There are multiple ways to loop through the elements present in the List, such as using for Loop, while loop, List Comprehension, enumerate function, range() function, or through the map() function. Among all these approaches, the most significant and simple way to loop through a Python List is “for loop”. This article has discussed seven ways to iterate/loop through a Python List.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×