Is there a 'foreach' function in Python 3?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python does not have a foreach keyword or function. The for loop in Python IS the foreach — it iterates directly over elements of any iterable (lists, tuples, dicts, sets, strings, generators) without needing an index. Where languages like PHP use foreach ($items as $item) or JavaScript uses items.forEach(callback), Python uses for item in items. This design is intentional — Python prefers a single, clear way to iterate.
Python's for Loop IS foreach
Iterating Over Different Types
With Index (enumerate)
Functional Alternatives
While Python prefers for loops, functional-style iteration exists:
Simulating forEach with Side Effects
If you want to apply a function to each element (like JavaScript's forEach):
Iterating in Parallel (zip)
Itertools for Advanced Iteration
Why Python Doesn't Have foreach
Python follows the "there should be one obvious way to do it" philosophy. The for loop already handles every iteration use case:
for x in list— iterate elementsfor i, x in enumerate(list)— iterate with indexfor k, v in dict.items()— iterate key-value pairsfor a, b in zip(l1, l2)— iterate in parallel
Adding a separate foreach would be redundant.
Common Pitfalls
- Modifying a list while iterating:
for item in items: items.remove(item)skips elements because the list changes under the iterator. Iterate over a copy (for item in items[:]) or use list comprehension for filtering. - Using
map()for side effects:map(print, items)is lazy — nothing prints until consumed. Use aforloop for side effects, notmap()or list comprehension. - Forgetting that
forover a dict iterates keys:for x in my_dictgives you keys, not values or key-value pairs. Use.values()or.items()for values or both. - Creating a list comprehension just for side effects:
[send_email(u) for u in users]creates an unnecessary list of return values. Use aforloop instead. - Using
range(len(list))instead ofenumerate:for i in range(len(items)): items[i]works but is less Pythonic and error-prone. Usefor i, item in enumerate(items)instead.
Summary
- Python's
for item in iterableIS the foreach — no separate keyword or function exists - Use
enumerate()when you need the index alongside the element - Use
zip()to iterate multiple iterables in parallel - Prefer
forloops overmap()or list comprehensions for side effects - List comprehensions are the Pythonic alternative to
map()andfilter()for transformations
Related reading
- Is there a function in Python which generates all the strings of length n over a given alphabet?
- Is there a label/goto in Python?
- Is there a library function for Root mean square error RMSE in python?
- Is there a list of Pytz Timezones?
- Is there a list of Pytz Timezones?
- Is there a math nCr function in Python?
- Is there a not equal operator in Python?
- Is there a NumPy function to return the first index of something in an array?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.