Quick way of getting the keys in a list of dictionaries [duplicate]
Asked Answered
B

4

5

Consider the example below:
m = [{'a':1},{'b':2}]
I wanted to find a short way of forming a list of the keys in m, just like ['a','b']. What would be the shortest or the easiest way rather than using traditional for loops? Perhaps a syntactic sugar?

Brute answered 17/5, 2019 at 9:18 Comment(0)
P
7

A simple double for-loop with list-comprehension should do the trick. Iterate over the list, and for each element in the list, iterate over the keys

In [5]: m = [{'a':1},{'b':2}]                                                                                                                                                                                                

In [6]: [k for item in m for k in item]                                                                                                                                                                                      
Out[6]: ['a', 'b']

If the list has duplicate keys, just convert the final output to a set and then to a list to get unique keys

In [19]: m = [{'a':1, 'b':2},{'a':3,'b':4}]                                                                                                                                                                                  

In [20]: r = [k for item in m for k in item]                                                                                                                                                                                 

In [21]: r                                                                                                                                                                                                                   
Out[21]: ['a', 'b', 'a', 'b']

In [22]: r = list(set(r))                                                                                                                                                                                                    

In [23]: r                                                                                                                                                                                                                   
Out[23]: ['b', 'a']
Phyto answered 17/5, 2019 at 9:22 Comment(0)
R
10

You can use list comprehension, a sintactic sugar of for loops:

keys_list = [x for d in m for x in d.keys()]

Note that if your dictionaries have keys in common they will be appear more than once in the result.

If you want only unique keys, you can do this:

keys_list = list(set(x for d in m for x in d.keys()))
Ralina answered 17/5, 2019 at 9:19 Comment(12)
But that does not give a string right? That gives something like [dict_keys(['a']), dict_keys(['b'])], instead of ['a','b']Brute
Yea, now i change the answe, sorry.Ralina
You can even write it more simply: [x for x in d for d in m] because iterating over a dictionary returns the keys.Castora
Thank you, didn't know. If you don't mind I edit the post with your suggestion.Ralina
I already have that approach in my answer @Ralina I think you might want to avoid duplication ?Phyto
Sorry, didn't see.Ralina
did you test this code ? @RalinaHurtless
Just corrected the error.Ralina
Not sure how the last approach gives unique keys, just try it with m = [{'a':1, 'b':2},{'a':3,'b':4}]Phyto
You are right, corrected it.Ralina
Cool, +1 for the correction. Could you upvote my answer as well :)Phyto
Yes, of course.Ralina
P
7

A simple double for-loop with list-comprehension should do the trick. Iterate over the list, and for each element in the list, iterate over the keys

In [5]: m = [{'a':1},{'b':2}]                                                                                                                                                                                                

In [6]: [k for item in m for k in item]                                                                                                                                                                                      
Out[6]: ['a', 'b']

If the list has duplicate keys, just convert the final output to a set and then to a list to get unique keys

In [19]: m = [{'a':1, 'b':2},{'a':3,'b':4}]                                                                                                                                                                                  

In [20]: r = [k for item in m for k in item]                                                                                                                                                                                 

In [21]: r                                                                                                                                                                                                                   
Out[21]: ['a', 'b', 'a', 'b']

In [22]: r = list(set(r))                                                                                                                                                                                                    

In [23]: r                                                                                                                                                                                                                   
Out[23]: ['b', 'a']
Phyto answered 17/5, 2019 at 9:22 Comment(0)
P
2
  1. Extract all dicts keys to a list of lists
  2. Convert them to one list by itertools.chain
from itertools import chain

a = [{'a': 1, 'c': 2}, {'b': 2}]
b = [d.keys() for d in a]
list(chain(*b))

will return:

['c', 'a', 'b']

Paolapaolina answered 17/5, 2019 at 9:23 Comment(0)
C
1

If you want just unique keys do this:

m = [{'a':1},{'b':2},{'b':3}]
print({x for d in m for x in d})

and this one contains duplicates:

print([x for d in m for x in d])
Compare answered 17/5, 2019 at 9:27 Comment(2)
did you test your code? @alireza yazdandoostHurtless
@Hurtless I edited that :) now it's okCompare

© 2022 - 2024 — McMap. All rights reserved.