Home > Mobile >  How save specific parts of a dict?
How save specific parts of a dict?

Time:03-05

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

CodePudding user response:

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

CodePudding user response:

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():
        for items in d.get("data").get("items"):
            for k, item in items.items():
                if k == "item":
                    temp = {"item": {k: v for k, v in item.items() if k == "id"}}
    id_item.append(temp)

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