Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract all keys from a list of dictionaries

Tags:

python

I'm trying to get a list of all keys in a list of dictionaries in order to fill out the fieldnames argument for csv.DictWriter.

previously, I had something like this:

[ {"name": "Tom", "age": 10}, {"name": "Mark", "age": 5}, {"name": "Pam", "age": 7} ] 

and I was using fieldnames = list[0].keys() to take the first dictionary in the list and extract its keys.

Now I have something like this where one of the dictionaries has more key:value pairs than the others (could be any of the results). The new keys are added dynamically based on information coming from an API so they may or may not occur in each dictionary and I don't know in advance how many new keys there will be.

[ {"name": "Tom", "age": 10}, {"name": "Mark", "age": 5, "height":4}, {"name": "Pam", "age": 7} ] 

I can't just use fieldnames = list[1].keys() since it isn't necessarily the second element that will have extra keys.

A simple solution would be to find the dictionary with the greatest number of keys and use it for the fieldnames, but that won't work if you have an example like this:

[ {"name": "Tom", "age": 10}, {"name": "Mark", "age": 5, "height":4}, {"name": "Pam", "age": 7, "weight":90} ] 

where both the second and third dictionary have 3 keys but the end result should really be the list ["name", "age", "height", "weight"]

like image 851
orh Avatar asked Jul 09 '12 16:07

orh


People also ask

How do I get all the keys in a dictionary list?

To get a list of all keys from a dictionary, you can simply use the dict. keys() function.

How do you get all keys from Python dictionary?

The dict. keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion.

What will list D keys ()) return?

You can also pass the dictionary to the list constructor, which is a shortcut to list(d. keys()) . Similarly, to get a list of the dictionary's values, you can pass the view returned by the dict. values() function to the list constructor.


2 Answers

all_keys = set().union(*(d.keys() for d in mylist)) 

Edit: have to unpack the list. Now fixed.

like image 187
Hugh Bothwell Avatar answered Sep 20 '22 00:09

Hugh Bothwell


Your data:

>>> LoD [{'age': 10, 'name': 'Tom'},   {'age': 5, 'name': 'Mark', 'height': 4},   {'age': 7, 'name': 'Pam', 'weight': 90}] 

This set comprehension will do it:

>>> {k for d in LoD for k in d.keys()} {'age', 'name', 'weight', 'height'} 

It works this way. First, create a list of lists of the dict keys:

>>> [list(d.keys()) for d in LoD] [['age', 'name'], ['age', 'name', 'height'], ['age', 'name', 'weight']] 

Then create a flattened version of this list of lists:

>>> [i for s in [d.keys() for d in LoD] for i in s] ['age', 'name', 'age', 'name', 'height', 'age', 'name', 'weight'] 

And create a set to eliminate duplicates:

>>> set([i for s in [d.keys() for d in LoD] for i in s]) {'age', 'name', 'weight', 'height'} 

Which can be simplified to:

{k for d in LoD for k in d.keys()} 
like image 26
dawg Avatar answered Sep 22 '22 00:09

dawg