Extract all keys from a list of dictionaries

前端 未结 6 767
遇见更好的自我
遇见更好的自我 2020-12-08 03:58

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 thi

相关标签:
6条回答
  • 2020-12-08 04:31

    The following example will extract the keys:

    set_ = set()
    for dict_ in dictionaries:
        set_.update(dict_.keys())
    print set_
    
    0 讨论(0)
  • 2020-12-08 04:32

    Borrowing lis from @AshwiniChaudhary's answer, here is an explanation of how you could solve your problem.

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

    Iterating directly over a dict returns its keys, so you don't have to call keys() to get them back, saving a function call and a list construction per element in your list.

    >>> {k for d in lis for k in d}
    set(['age', 'name', 'weight', 'height'])
    

    or use itertools.chain:

    >>> from itertools import chain
    >>> {k for k in chain(*lis)}
    set(['age', 'name', 'weight', 'height'])
    
    0 讨论(0)
  • 2020-12-08 04:39
    all_keys = set().union(*(d.keys() for d in mylist))
    

    Edit: have to unpack the list. Now fixed.

    0 讨论(0)
  • 2020-12-08 04:40
    from itertools import chain
    
    lis = [
        {"name": "Tom", "age": 10},
        {"name": "Mark", "age": 5, "height":4},
        {"name": "Pam", "age": 7, "weight":90}
    ]
    
    # without qualification a dict iterates over its keys
    # and set takes any iterable in its constructor
    headers_as_set = set(chain.from_iterable(lis))
    
    # you asked for a list
    headers = list(
        set(chain.from_iterable(lis))
    )
    
    0 讨论(0)
  • 2020-12-08 04:41
    >>> lis=[
    {"name": "Tom", "age": 10},
    {"name": "Mark", "age": 5, "height":4},
    {"name": "Pam", "age": 7, "weight":90}
    ]
    >>> {z for y in (x.keys() for x in lis) for z in y}
    set(['age', 'name', 'weight', 'height'])
    
    0 讨论(0)
  • 2020-12-08 04:51

    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()}
    
    0 讨论(0)
提交回复
热议问题