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}]
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}]
You could do that with list comprehension,
active_users = [user for user in users if user['active']]
Using a list comprehension would work fine:
users = [user for user in users if user['active']]
Use filter. It will return a list in py2.x and a generator for py3.x
filter(lambda x:x['active'], users)