Christopher Tyler

Use filter() instead of for loop

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:

1
2
3
4
filtered = []
for item in my_list:
    if condition(item):
        filtered.append(item)

Do this instead:

filtered = list(filter(condition, my_list))

<< Previous Post

|

Next Post >>

#Python