Use filter() instead of for loop
Use filter() instead of for loop
October 18, 2025
From https://dev.to/0x3d_site/python-shortcuts-that-save-you-hours-5dfp. While the overall tip was to try and use Python’s built-ins more, below was a very useful tip for me. Instead of this:
filtered = []
for item in my_list:
if condition(item):
filtered.append(item)
Do this instead:
filtered = list(filter(condition, my_list))