0

I have this dictionary, where I would like to retrieve the value of 'id'. What would be the best way?

dict = {'total': 2, 'page': 0, 'pageSize': 10, 'count': 2, 'results': [{'id': 3, 'domainId': 1}]}

I have tried:

      print (dict['id'])

E KeyError: 'id'

if I print the keys, I only get:

['total', 'page', 'pageSize', 'count', 'results']

5 Answers 5

1

First things first, don't call your variable dict as that is the name of the Python type of a dictionary, so you're currently overwriting that function.

Let's call it d instead.

d = {
  'total': 2, 
  'page': 0, 
  'pageSize': 10, 
  'count': 2, 
  'results': [
    {'id': 3, 'domainId': 1}
  ]
}

Here we can see that d['results'] is what we want to query for the id. It's a list, so I would question whether you're always going to want the first thing or all of the things or just the last thing.

If it were me, I would first extract all ids, and then get the one I actually wanted.

We can get all ids as a list with:

[r['id'] for r in d['results'] if 'id' in r]

That means that if results was the following list, we'd get the following ids:

d['results'] == [
  {'id': 3, 'domainId': 1},
  {'id': 4},
  {'domainId': 2},
  {'giraffe': True}
]

ids = [r['id'] for r in d['results'] if 'id' in r]

ids == [3, 4]

At this point we could process all ids, or just get the first (ids[0]) or the last (ids[-1]).


If you know exactly which index it's going to be in results, you could access that directly.

d['results'][-1]['id']
Sign up to request clarification or add additional context in comments.

Comments

0

id_value = dict['results'][0]['id']

Also, you generally shouldn't call your dictionaries dict in Python. dict is the name of a built-in function, and you might mess things up by overwriting it.

Comments

0

dict['results'][0]['id'] gets that value. Having the nested dictionary in a list convolutes things a bit.

4 Comments

and what if I want to retrieve all 'id' values in case I have multiple? I mean: what if I have: d = {'total': 2, 'page': 0, 'pageSize': 10, 'count': 2, 'results': [{'id': 3, 'domainId': 1},{'id': 4, 'domainId': 1}]}. how do I retrieve the values 3,4 from here?
dict['results'][0]['id'], dict['results'][1]['id']. I would advise against storing data like this as it isn't a very clear way of retrieving it. Maybe a class or a pandas DataFrame would be more suited?
and what is the best way to use instead of [0] and [1] while knowing the amount of objects I would like to retrieve? meaning, I know the count is 2 in this case, but I can't just do [1] and [2] as I would need in this case, use [0] and [1]. would it be a for loop starting from 0 until [count-1] be the best way?
[x['id'] for x in dict['results'][:count]] could be an option. But again I would try and rethink your data structure.
0
value = dict['results'][0]['id']

You use the dict['results'] to access the list in which the first element [0] that is a dictionary in which you can finally get the value of [id] .

Comments

0

The value of 'results' is a list whose first item is another dictionary. You can access the nested dictionary with index 0.

print(dict['results'][0]['id'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.