Reputation: 29
I need to choose all active users with the easiest way
users = [{'id': 1, 'active': False}, {'id': 2, 'active': True}, {'id': 3, 'active': True}, {'id': 4, 'active': True}, {'id': 5, 'active': True}, {'id': 6, 'active': True}]
Upvotes: 0
Views: 233
Reputation: 20349
Use filter. It will return a list in py2.x and a generator for py3.x
filter(lambda x:x['active'], users)
Upvotes: 2
Reputation: 5574
Using a list comprehension would work fine:
users = [user for user in users if user['active']]
Upvotes: 2
Reputation: 9235
You could do that with list comprehension,
active_users = [user for user in users if user['active']]
Upvotes: 2