Construct a dictionary from a list of dictionary keys, sub-keys and values

reservoirinvest

Have a list of dictionaries:

list_dict = [['key1', {'subkey1': 0}],
             ['key1', {'subkey2': 2}],
             ['key1', {'subkey5': 5}],
             ['key2', {'subkey2': 4}],
             ['key2', {'subkey1': 8}],
             ['key1', {'sybkey5': 10}]]

How can list_dict be converted to a neat dictionary as follows?

{'key1': {'subkey1': 0, 'subkey2': 2, 'subkey5': 5},
 'key2': {'subkey2': 4, 'subkey1': 8, 'sybkey5': 10}}
Stefan Scheller

The following should work:

d = dict()
for item in list_dict:
    d.setdefault(item[0], {}).update(item[1])

From the Python 3 Documentation:

setdefault(key[, default])

If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Assign list values to dictionary keys

分類Dev

Swap keys for values in dictionary?

分類Dev

Filling a dataframe from a dictionary keys and values

分類Dev

retrieve values from dataframe using keys in dictionary

分類Dev

Creating a dictionary with values identical to keys from another dictionary

分類Dev

Compare keys of dictionary with values of another dictionary

分類Dev

Assigning values to keys in a mutable dictionary

分類Dev

Match keys to values in dictionary in python

分類Dev

How to convert dictionary to dataframe when values of keys are list of list?

分類Dev

Construct tuple from dictionary values

分類Dev

Search dictionary values and return List of keys meeting conditions

分類Dev

How to convert a dictionary to a list of keys, with repeat counts given by the values?

分類Dev

How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

分類Dev

Create dictionary from/with function arguments as keys and default values as value

分類Dev

keep only keys without None values in dictionary from pandas groups

分類Dev

From Pandas series, create dictionary with unique elements as keys, and their indices as values

分類Dev

Filling a dataframe from a dictionary keys and values: efficient way

分類Dev

add keys and values to a dictionary from multiple objects by for loop

分類Dev

How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

分類Dev

How to add multiple values to dictionary keys in Python

分類Dev

Edit nested dictionary duplicate values in same keys

分類Dev

How to sum up values of duplicate keys in a dictionary?

分類Dev

How to get dictionary keys and values if both keys are in two separated dictionaries?

分類Dev

lookup/map multiple keys in a dictionary and add to a list

分類Dev

Write a csv file from a dictionary with compound keys

分類Dev

Converting pandas dataframe into dictionary where keys are index and values are list of column values

分類Dev

Strategy for dictionary with optional keys

分類Dev

Moving Keys in dictionary

分類Dev

How to update keys in dictionary?

Related 関連記事

  1. 1

    Assign list values to dictionary keys

  2. 2

    Swap keys for values in dictionary?

  3. 3

    Filling a dataframe from a dictionary keys and values

  4. 4

    retrieve values from dataframe using keys in dictionary

  5. 5

    Creating a dictionary with values identical to keys from another dictionary

  6. 6

    Compare keys of dictionary with values of another dictionary

  7. 7

    Assigning values to keys in a mutable dictionary

  8. 8

    Match keys to values in dictionary in python

  9. 9

    How to convert dictionary to dataframe when values of keys are list of list?

  10. 10

    Construct tuple from dictionary values

  11. 11

    Search dictionary values and return List of keys meeting conditions

  12. 12

    How to convert a dictionary to a list of keys, with repeat counts given by the values?

  13. 13

    How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

  14. 14

    Create dictionary from/with function arguments as keys and default values as value

  15. 15

    keep only keys without None values in dictionary from pandas groups

  16. 16

    From Pandas series, create dictionary with unique elements as keys, and their indices as values

  17. 17

    Filling a dataframe from a dictionary keys and values: efficient way

  18. 18

    add keys and values to a dictionary from multiple objects by for loop

  19. 19

    How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

  20. 20

    How to add multiple values to dictionary keys in Python

  21. 21

    Edit nested dictionary duplicate values in same keys

  22. 22

    How to sum up values of duplicate keys in a dictionary?

  23. 23

    How to get dictionary keys and values if both keys are in two separated dictionaries?

  24. 24

    lookup/map multiple keys in a dictionary and add to a list

  25. 25

    Write a csv file from a dictionary with compound keys

  26. 26

    Converting pandas dataframe into dictionary where keys are index and values are list of column values

  27. 27

    Strategy for dictionary with optional keys

  28. 28

    Moving Keys in dictionary

  29. 29

    How to update keys in dictionary?

ホットタグ

アーカイブ