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

Python List Items Iterating

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:

  1. Using the for loop
  2. Using the enumerate() function
  3. List Comprehension
  4. Using the map() function
  5. Using the filter() function

1. Using the for loop

A 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

2. Using the enumerate() function

The 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

3. List Comprehension

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]

4. Using the map() function

The 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]

5. Using the filter() function

The 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.

  1. Using a for loop to iterate through a list in Python:

    • Description: Iterating through a list using a for loop is a fundamental and common approach.
    • Example Code:
      my_list = [1, 2, 3, 4, 5]
      for item in my_list:
          print(item)
      
  2. Iterating over elements of a list in Python:

    • Description: Iterating over each element in a list using a for loop.
    • Example Code:
      my_list = ['apple', 'orange', 'banana']
      for fruit in my_list:
          print(fruit)
      
  3. How to loop through a Python list:

    • Description: Looping through a list can be achieved using various constructs, with a for loop being the most common.
    • Example Code:
      my_list = [6, 7, 8, 9, 10]
      for number in my_list:
          print(number)
      
  4. Accessing and iterating list items in Python:

    • Description: Accessing and iterating through items in a list using a for loop.
    • Example Code:
      my_list = ['cat', 'dog', 'rabbit']
      for pet in my_list:
          print("I have a", pet)
      
  5. List comprehension for iterating over a list in Python:

    • Description: List comprehension provides a concise way to iterate over a list and create a new list.
    • Example Code:
      my_list = [11, 12, 13, 14, 15]
      squared_values = [x**2 for x in my_list]
      print(squared_values)
      
  6. Using enumerate to iterate with index over a list in Python:

    • Description: enumerate allows iterating over both the index and value of items in a list.
    • Example Code:
      my_list = ['apple', 'orange', 'banana']
      for index, fruit in enumerate(my_list):
          print(f"Index {index}: {fruit}")
      
  7. Iterating backwards through a list in Python:

    • Description: Reversing a list and then iterating through it in reverse order using a for loop.
    • Example Code:
      my_list = [16, 17, 18, 19, 20]
      for number in reversed(my_list):
          print(number)
      
  8. Iterating over a subset of list items in Python:

    • Description: Using slicing to iterate over a subset of items in a list.
    • Example Code:
      my_list = ['red', 'green', 'blue', 'yellow', 'orange']
      for color in my_list[1:4]:
          print(color)