简体   繁体   中英

iterate through nested dictionary Python

How Do I filter and return the users who are enabled? In my initial approach I'm able to get the all 3 items
{'name': 'ijk', 'age': 32, 'enabled': 'true'}

response = {
    "users": [
        {
            "name": "abc",
            "age": 25,
            "enabled": 'true'
        },
        {
            "name": "def",
            "age": 28,
            "enabled": 'false'
        },
        {
            "name": "ijk",
            "age": 32,
            "enabled": 'true'
        }
    ]
}

enabled_users = []
for user in response["users"]:
    if user['enabled'] == 'true':
        enabled_users.append(user)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM