Python 3.x Filter List: Comprehension, Lambda and Generator

Apr 9, 2020
items = [    {'name': 'Desmond', 'age': 40},    {'name': 'Elon', 'age': 48},    {'name': 'Jack', 'age': 43}]

Comprehension

new_items = [item for item in items if item['age'] >= 45]

Lambda

new_items = filter(Lambda x: x.age >= 45, items)

Generator

def filter_items(items):    for item in items:        if item['age'] >= 45            yield itemnew_items = list(filter_items(items))

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.