Open In App

Python Foreach - How to Implement ?

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Foreach loop is a convenient way to iterate over elements in a collection, such as an array or list. Python, however, does not have a dedicated foreach loop like some other languages (e.g., PHP or JavaScript). We will explore different ways to implement a foreach-like loop in Python, along with examples to illustrate their usage.

Using the For Loop using Python 'in'

The most straightforward way to iterate over elements in a collection in Python is by using the for loop. This loop automatically iterates over each item in the collection, providing a clean and readable way to access each element.

Python
a = [1, 2, 3, 4, 5]

for n in a:
    print(n)

Output
1
2
3
4
5

Example: Let's consider a practical example where we have a list of dictionaries representing students, and we want to print each student's name and grade.

Python
s = [
    {"name": "RAM", "grade": "A"},
    {"name": "SITA", "grade": "B"},
    {"name": "GITA", "grade": "C"}
]

for student in s:
    print(f"Name: {student['name']}, Grade: {student['grade']}")

Output
Name: RAM, Grade: A
Name: SITA, Grade: B
Name: GITA, Grade: C

Explanation: This code iterates through a list of dictionaries (s), where each dictionary contains a student's name and grade. For each student, it prints their name and grade using an f-string.

Using the map Function

The map function in Python applies a specified function to each item in an iterable (such as a list) and returns a map object (which is an iterator). This approach is particularly useful when you want to apply a transformation or function to each element in the collection.

Python
def square(n):
    return n * n

a = [1, 2, 3, 4, 5]
sn = map(square, a)

for n in sn:
    print(n)

Output
1
4
9
16
25

Explanation: map(square, a) applies the square() function to every element in the list a and returns a map object (sn), which is an iterable.

Using List Comprehensions

List comprehensions provide a concise way to create lists and can also be used to iterate over elements in a collection. While list comprehensions are typically used for creating new lists, they can also be utilized to perform actions on each element.

Python
a = [1, 2, 3, 4, 5]

[print(num) for num in a]

Output
1
2
3
4
5

Using the itertools Module

The itertools module in Python provides a variety of tools for working with iterators. One of the functions, starmap, can be used to apply a function to the elements of an iterable. This is particularly useful for iterating over elements that are tuples or pairs.

Python
from itertools import starmap

p = [(1, 2), (3, 4), (5, 6)]

def add(a, b):
    return a + b

r = starmap(add, p)

for res in r:
    print(res)

Output
3
7
11

Explanation: This code uses itertools.starmap() to apply the add() function to each tuple in the list p. The starmap() function unpacks each tuple in p and passes the elements as arguments to the add() function.


Article Tags :
Practice Tags :

Similar Reads