How to Iterate Through a List in Python?

1 week ago 9
Club.noww.in

While working with Python and machine learning, one of my team members asked about loop-through lists in Python. There are various methods to do this. In this tutorial, I will explain how to iterate through a list in Python using different methods and examples.

To iterate through a list in Python, the most straightforward method is using a for loop. The syntax is simple: for item in list_name:, where item represents each element in the list, and list_name is the list you’re iterating over. For example, if you have a list of city names like cities = ["New York", "Los Angeles", "Chicago", "Houston"], you can print each city by writing for city in cities: print(city).

Iterate Through a List in Python

A list in Python is an ordered collection of items that can be of different data types. Lists are mutable, meaning you can change their content without changing their identity. Here’s a quick example of how to create a list:

names = ["Alice", "Bob", "Charlie", "David"]

Now, let me show you different methods to iterate through a list in Python.

Method 1: Using a for Loop

The for loop is one of the simplest and most common ways to iterate through a list in Python. Here’s the basic syntax:

for item in list_name: # Do something with item

Example:

Let’s say we have a list of city names, and we want to print each city:

cities = ["New York", "Los Angeles", "Chicago", "Houston"] for city in cities: print(city)

Output:

New York Los Angeles Chicago Houston

I executed the above Python code using VS code, and you can see the output in the screenshot below:

python iterate list

Check out Convert String to List in Python

Method 2: Using a while Loop

A while loop can also be used to iterate through a list in Python, although it’s less common than the for loop. The while loop continues as long as a specified condition is true.

Example:

cities = ["New York", "Los Angeles", "Chicago", "Houston"] index = 0 while index < len(cities): print(cities[index]) index += 1

Output:

New York Los Angeles Chicago Houston

Here is the output in the screenshot below:

python for each in list

Read Merge Lists Without Duplicates in Python

Method 3: List Comprehension

List comprehension is a concise way to create lists and iterate through them. It is often used for creating new lists by applying an expression to each item in an existing list.

Example:

Suppose we want to create a list of city names in uppercase:

cities = ["New York", "Los Angeles", "Chicago", "Houston"] uppercase_cities = [city.upper() for city in cities] print(uppercase_cities)

Output:

['NEW YORK', 'LOS ANGELES', 'CHICAGO', 'HOUSTON']

Method 4: Using enumerate()

The enumerate() function adds a counter to an iterable and returns it as an enumerate object. This can be particularly useful when you need both the index and the value of each item in the list.

Example:

cities = ["New York", "Los Angeles", "Chicago", "Houston"] for index, city in enumerate(cities): print(f"City {index + 1}: {city}")

Output:

City 1: New York City 2: Los Angeles City 3: Chicago City 4: Houston

You can see the exact output in the screenshot below:

python loop through list

Read Convert a Dictionary to a List in Python

Method 5: Using map()

The map() function in Python applies a given function to all items in an input list. This is useful when you need to apply the same operation to every item in the list.

Example:

cities = ["New York", "Los Angeles", "Chicago", "Houston"] lengths = list(map(len, cities)) print(lengths)

Output:

[8, 11, 7, 7]

Method 6: Using filter()

The filter() function constructs an iterator from elements of an iterable for which a function returns true. This is useful for filtering items in a list based on a condition.

Example:

cities = ["New York", "Los Angeles", "Chicago", "Houston"] long_cities = list(filter(lambda city: len(city) > 7, cities)) print(long_cities)

Output:

['New York', 'Los Angeles']

You can see the exact output in the screenshot below:

iterate through list python

Conclusion

In this tutorial, I explained how to iterate through lists in Python using different methods with examples. I have shown examples for each method like:

  • Using a for Loop
  • Using a while Loop
  • List Comprehension
  • Using enumerate()
  • Using map()
  • Using filter()

You may also like:

Read Entire Article