简体   繁体   中英

How save specific parts of a dict?

i've a list that looks like this

for i in negocio:   
params = {
    'term': i,
    'fields': 'title',
    'exact_match': 'true'}
response = client.deals.search_deals(params=params)
deal_id.append(response)


deal_id

after deal_id run, i receive back these list.

    [{'success': True,
  'data': {'items': [{'result_score': 1.28568,
     'item': {'id': 151897,
      'type': 'deal',
      'title': 'deal_1',
      'value': None,
      'status': 'open',
      'visible_to': 7,
      'owner': {'id': 13863990},
    
      'person': {'id': 209102,
      
      'organization': None,
      'custom_fields': [],
      'notes': []}}]},
  'additional_data': {'pagination': {'start': 0,
    'limit': 100,
    'more_items_in_collection': False}}},
 {'success': True,
  'data': {'items': [{'result_score': 1.28568,
     'item': {'id': 151898,
      'type': 'deal',
      'title': 'deal_2',
      'value': None,
      'status': 'open',
      'visible_to': 7,
      'owner': {'id': 13863990},
     
      'person': {'id': 331122,

      'organization': None,
      'custom_fields': [],
      'notes': []}}]},
  'additional_data': {'pagination': {'start': 0,
    'limit': 100,
    'more_items_in_collection': False}}}]

How can i keep just the numbers inside these pieces of my list 'item': {'id': 151897} , and 'item': {'id': 151898}

I looked on similar topics but don't found a anwser that could help me

Save this info in a variable:

x = mylist[0]['key name']

then you can delete that key with:

my_list[0]['key name']

And add a new one. Or generate the same dict without that key. Check this POST

You should check the deal_id . It gave me error, so I fixed it manually.

my_list = [
    {
        "success": True,
        "data": {
            "items": [
                {
                    "result_score": 1.28568,
                    "item": {
                        "id": 151897,
                        "type": "deal",
                        "title": "deal_1",
                        "value": None,
                        "status": "open",
                        "visible_to": 7,
                        "owner": {"id": 13863990},
                        "person": {
                            "id": 209102,
                            "organization": None,
                            "custom_fields": [],
                            "notes": [],
                        },
                    },
                }
            ] # probably the problem is here
        },
        "additional_data": {
            "pagination": {"start": 0, "limit": 100, "more_items_in_collection": False}
        },
    },
    {
        "success": True,
        "data": {
            "items": [
                {
                    "result_score": 1.28568,
                    "item": {
                        "id": 151898,
                        "type": "deal",
                        "title": "deal_2",
                        "value": None,
                        "status": "open",
                        "visible_to": 7,
                        "owner": {"id": 13863990},
                        "person": {
                            "id": 331122,
                            "organization": None,
                            "custom_fields": [],
                            "notes": [],
                        },
                    },
                }
            ]
        },
        "additional_data": {
            "pagination": {"start": 0, "limit": 100, "more_items_in_collection": False}
        },
    },
]

If you want to delete the unwanted key you should check inside each loop and remove them. Otherwise you can just save the wanted value, like below.

id_item = []
for d in my_list:
    for k, v in d.items():
        if isinstance(v, dict):
            for k, vl in v.items():
                # maybe you need more loop iof there are more than one [item {...}]
                if vl and isinstance(vl, list):
                    temp = {"item": {"id": vl[0].get("item").get("id")}}
    id_item.append(temp)

print(id_item)
# [{'item': {'id': 151897}}, {'item': {'id': 151898}}]

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