Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python List Items Iterating tutorial, we'll cover different ways to iterate through a list in Python. We will focus on the following methods:
for
loopenumerate()
functionmap()
functionfilter()
functionfor
loopA for
loop is the most common way to iterate through a list in Python. Here's an example:
my_list = [1, 2, 3, 4, 5] for item in my_list: print(item) # Output: # 1 # 2 # 3 # 4 # 5
enumerate()
functionThe enumerate()
function allows you to iterate through a list while also keeping track of the index of the current item.
Example:
my_list = ['apple', 'banana', 'cherry'] for index, item in enumerate(my_list): print(index, item) # Output: # 0 apple # 1 banana # 2 cherry
List comprehension is a concise way to create a new list by applying an expression to each element in an existing list.
Example:
my_list = [1, 2, 3, 4, 5] squares = [x ** 2 for x in my_list] print(squares) # Output: [1, 4, 9, 16, 25]
map()
functionThe map()
function applies a specified function to each item in an iterable (e.g., list) and returns a map object.
Example:
my_list = [1, 2, 3, 4, 5] def square(x): return x ** 2 squares = map(square, my_list) print(list(squares)) # Output: [1, 4, 9, 16, 25]
filter()
functionThe filter()
function filters the elements of an iterable based on a function that returns a boolean value (True or False).
Example:
my_list = [1, 2, 3, 4, 5] def is_even(x): return x % 2 == 0 even_numbers = filter(is_even, my_list) print(list(even_numbers)) # Output: [2, 4]
In this tutorial, you learned different ways to iterate through a list in Python, including using a for
loop, the enumerate()
function, list comprehension, and the map()
and filter()
functions.
Using a for loop to iterate through a list in Python:
for
loop is a fundamental and common approach.my_list = [1, 2, 3, 4, 5] for item in my_list: print(item)
Iterating over elements of a list in Python:
for
loop.my_list = ['apple', 'orange', 'banana'] for fruit in my_list: print(fruit)
How to loop through a Python list:
for
loop being the most common.my_list = [6, 7, 8, 9, 10] for number in my_list: print(number)
Accessing and iterating list items in Python:
for
loop.my_list = ['cat', 'dog', 'rabbit'] for pet in my_list: print("I have a", pet)
List comprehension for iterating over a list in Python:
my_list = [11, 12, 13, 14, 15] squared_values = [x**2 for x in my_list] print(squared_values)
Using enumerate to iterate with index over a list in Python:
enumerate
allows iterating over both the index and value of items in a list.my_list = ['apple', 'orange', 'banana'] for index, fruit in enumerate(my_list): print(f"Index {index}: {fruit}")
Iterating backwards through a list in Python:
for
loop.my_list = [16, 17, 18, 19, 20] for number in reversed(my_list): print(number)
Iterating over a subset of list items in Python:
my_list = ['red', 'green', 'blue', 'yellow', 'orange'] for color in my_list[1:4]: print(color)