Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Iterate a List in Python

Learn how to iterate over lists, tuples, and other iterable objects in Python with ease.| …


Updated June 17, 2023

|Learn how to iterate over lists, tuples, and other iterable objects in Python with ease.|

How to Iterate a List in Python

Definition of the Concept

Iteration is a fundamental concept in programming that allows you to access each element in a sequence (such as a list or tuple) one at a time. In Python, iteration is used extensively when working with lists, tuples, strings, and other iterable objects.

Step-by-Step Explanation


Using the for Loop

The most common way to iterate over a list in Python is by using a for loop:

# Create a sample list
my_list = [1, 2, 3, 4, 5]

# Iterate over the list using a for loop
for item in my_list:
    print(item)

Output:

1
2
3
4
5

In this example:

  • my_list is the iterable object (a list) that we want to iterate over.
  • The for loop iterates over each element in the list, assigning it to the variable item.
  • The print(item) statement inside the loop prints each item.

Understanding Loop Variables

Note that the variable item used in the for loop is not a special keyword; it’s simply a regular Python variable. This means you can use any valid Python identifier as your loop variable.

Iterating Over Indices (Optional)

If you need to access both the element and its index during iteration, you can use the enumerate function:

# Create a sample list
my_list = [1, 2, 3, 4, 5]

# Iterate over the list using enumerate
for idx, item in enumerate(my_list):
    print(f"Index: {idx}, Value: {item}")

Output:

Index: 0, Value: 1
Index: 1, Value: 2
Index: 2, Value: 3
Index: 3, Value: 4
Index: 4, Value: 5

In this case:

  • enumerate takes an iterable object and returns a tuple containing the index and value for each element.
  • We use the for loop to iterate over these tuples.

Using Other Iteration Methods

While the for loop is the most common iteration method in Python, you can also use other techniques:

List Comprehensions

List comprehensions provide a concise way to create new lists based on existing ones:

# Create a sample list
my_list = [1, 2, 3, 4, 5]

# Use a list comprehension to create a new list with squares
squares = [x ** 2 for x in my_list]
print(squares)

Output: [1, 4, 9, 16, 25]

While it’s technically possible to iterate over a list using indexing (i.e., my_list[i]), this approach is not recommended for most use cases:

# Create a sample list
my_list = [1, 2, 3, 4, 5]

# Iterate over the list using indexing
for i in range(len(my_list)):
    print(my_list[i])

This method has several drawbacks:

  • It’s less readable and maintainable than using a for loop.
  • It requires explicit indexing (e.g., i) instead of letting Python handle it for you.

Conclusion

Iterating over lists, tuples, and other iterable objects is an essential skill in Python programming. By mastering the techniques outlined in this article, you’ll be able to write more efficient, readable, and maintainable code. Remember to use the for loop whenever possible and avoid indexing-based iteration unless absolutely necessary.

Additional Resources

For further learning, explore these resources:

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp