0

I have a list of dictionaries
input:

x = [{'id': 19, 'number': 123, 'count': 1}, 
     {'id': 1, 'number': 23, 'count': 7}, 
     {'id': 2, 'number': 238, 'count': 17},
     {'id': 1, 'number': 9, 'count': 1}]

How would I get the list of number:

[123, 23, 238, 9]

Thank you for you reading

1
  • 1
    Try: [i['number'] for i in x] Commented Jun 9, 2022 at 17:26

3 Answers 3

2

To get these numbers you can use

>>> [ d['number'] for d in x ]

But this is not the "list of keys" for which you ask in the question title. The list of keys of each dictionary d in x is obtained as d.keys() which would yield something like ['id', 'number', ...]. Do for example

>>> [ list(d.keys()) for d in x ]

to see. If they are all equal you are probably only interested in the first of these lists. You can get it as

>>> list( x[0].keys() )

Note also that the "elements" of a dictionary are actually the keys rather than the values. So you will also get the list ['id', 'number',...] if you write

>>> [ key for key in x[0] ]

or simply (and better):

>>> list( x[0] )

To get the first element is more tricky when x is not a list but a set or dict. In that case you can use next(x.__iter__()).

P.S.: You should actually think what you really want the keys to be -- a priori that should be the 'id's, not the 'number's, but your 'id's have duplicates which is contradictory to the concept and very definition / meaning of 'id' -- and then use the chosen keys as identifiers to index the elements of your collection 'x'. So if the keys are the 'number's, you should have a dictionary (rather than a list)

x = {123: {'id': 19, 'count': 1}, 23: {'id': 1, 'count': 7}, ...}

(where I additionally assumed that the numbers are indeed integers [which is more efficient] rather than strings, but that's up to you). Then you can also do, e.g., x[123]['count'] += 1 to increment the 'count' of entry 123.

Sign up to request clarification or add additional context in comments.

3 Comments

there is no need for .keys() ... also, [ key for key in x[0] ] should just not be there, anything of the form [x for x in whatever] should just be list(whatever)
That's exactly what I explain in my answer! The keys() attribute is there for a reason -- it is (obviously, by definition) the "official" way to get the keys, so I mention it, but then I proceed by showing that iterating over the "elements" of a dict actually iterates over the keys (which is counter-intuitive for unexperienced programmers, they consider keys as indices and iterating over elements of a container shouldn't run over indices but over values) -- and finally I say that this is obtained by list ( [the dict] ).
PS: if you re-read my answer, it was also to demonstrate that the actual keys are not the "keys" the OP asked for in the question (title). If I just write list( x[0] ) this does not show or "prove" that the result is indeed the keys of the dict, therefore I first wrote [ list( d.keys()) for d in x] to make it obvious and beyond any doubt, what the keys of the elements of his list of dicts (?!) are.
1

You can use a list comprehension:

numbers = [dictionary.get('number') for dictionary in list_of_dictionaries]

1 Comment

You can also do dictionary['number'] in place of dictionary.get('number')
1

Using a functional programming approach:

from operator import itemgetter

x_k = list(map(itemgetter('number'), x))
#[123, 23, 238, 9]

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.