简体   繁体   中英

Forming List Comprehension For Dictionary Inside a List of Dictionaries

I have a webservice json response which is a list of dictionaries and the values are themselves dictionaries. Like the example below:

[
 {
   "ET": {
          "application_environment": "Y",
          "period_process": "Y",
          "extended_options": "N",
          "reports": "Y",
          "administration_tasks": "N",
          "daily_process": "Y",
          "inquiry": "Y"
          }
 },
 {
   "SD": {
           "daily_process": "Y",
           "authorize_cashier_to_close_warranty": "Y",
           "authorize_to_update_technicians_cost": "Y",
           "authorize_to_booking": "Y",
           "authorize_to_policy_adjustments": "Y",
           "authorize_to_pre-paid_maintenance_packages": "Y",
           "authorize_to_enter_actual_time": "Y"
          }
  }
]

I have the following which works, I am just wondering if there is a way to do this with list comprehension?

    parsed_response = response.json()
    from_svc = []
    for items in parsed_response:
        for values in items.values():
            for daily_process in values.get("daily_process"):
                from_svc.append(daily_process)

I think this is what you're looking for?

[ values['daily_process'] for items in parsed_response 
                              for values in items.values() ]

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