kenzo
kenzo

Reputation: 29

Check all active users from array

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

Answers (4)

im_w0lf
im_w0lf

Reputation: 144

active_user = [user['id']  for user in users if user['active']]

Upvotes: 0

itzMEonTV
itzMEonTV

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

aumo
aumo

Reputation: 5574

Using a list comprehension would work fine:

users = [user for user in users if user['active']]

Upvotes: 2

zaidfazil
zaidfazil

Reputation: 9235

You could do that with list comprehension,

active_users = [user for user in users if user['active']]

Upvotes: 2

Related Questions