0

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

  • Does this answer your question? [How can I remove a key from a Python dictionary?](https://stackoverflow.com/questions/11277432/how-can-i-remove-a-key-from-a-python-dictionary) –  Mar 04 '22 at 15:58
  • Delete all other keys from the list is more easily than create a new list with what i want? – Gustavo Lima Mar 04 '22 at 16:01
  • try edit your question first! `my_list` has problem. simply use a loop, to get all the dict, then use `.items()` method and use an if condition to `.pop()` the keys that you do not want. – A D Mar 04 '22 at 16:06
  • these are not working cause i only've these three keys. dict_keys(['success', 'data', 'additional_data']), if you look well, things are being created inside others – Gustavo Lima Mar 04 '22 at 16:29

2 Answers2

0

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

  • This also dont work, i think that's because i'only have 3 keys, = dict_keys(['success', 'data', 'additional_data']) – Gustavo Lima Mar 04 '22 at 16:43
0

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}}]
A D
  • 585
  • 1
  • 7
  • 16
  • I need exactly this. but in this piece of code for items in d.get("data").get("items"): i receive back a Attribute error: 'Nonetype' object has no attribute get – Gustavo Lima Mar 04 '22 at 18:30
  • Because your `deal_id ` has problem. As I told, it gave me error. `File "/....py", line 232 'notes': []}}]}, ^ SyntaxError: closing parenthesis ']' does not match opening parenthesis '{' on line 219` So I fixed it manually (the snippets `my_list`). Probably `search_deals` does not create the `dict`s correctly. can you give us the value of `negocio`? – A D Mar 04 '22 at 19:23
  • Oh, i see why, is because have some data about clients that i deleted before post here, deal_id cant be wrong because come from other iteration completely. But negocio is a list of id's that comes from a other iteration inside a internal file. – Gustavo Lima Mar 04 '22 at 19:32
  • List index out of range. Sorry ask so many times – Gustavo Lima Mar 04 '22 at 19:47
  • You shohld check the length of the `vl`. I have updated the answer. – A D Mar 04 '22 at 20:07