Unpacking a list of dictionaries based on key matches in Python

MikG

I am looking for a way to unpack a list of dictionaries with a certain key ID. I have seen a lot of examples based on a key value, but nothing on just a key match. Say I have a dictionary in the following format returned from a function...

data = [{'Lev1': u'82', 'Marker': u'16', 'TYPE': u'139', 'Location': u'A'},
        {'Lev2': u'652', 'Marker': u'1', 'TYPE': u'140', 'Location': u'C'},
        {'Lev3': u'452', 'Marker': u'188', 'TYPE': u'141', 'Location': u'B'}]

My current attempt is shown below, but I am getting >> TypeError: list indices must be integers, not str

for item in data:
    parts[data['TYPE']].update(data)

The parts reference above is a dictionary of parts numbers. I am looking to drop each of the following list entries (for example, 'Lev1': u'82', 'Marker': u'16', 'TYPE': u'139', 'Location': u'A') into the main 'parts' dictionary based on a TYPE (there should already be a TYPE match in the parts dictionary).

My method works for a single returned dictionary entry...

parts[data['TYPE']].update(data)

...but just not with a list of dictionaries.

I am looking to end up with a format along the lines of...

parts{
    125:
    ...
    ...
    ...
    139:{
        'Lev1': u'82', 
        'Marker': u'16', 
        'TYPE': u'139', 
        'Location': u'A'
        plus other previously gathered data
        }

    140:{
        'Lev2': u'652', 
        'Marker': u'1', 
        'TYPE': u'140', 
        'Location': u'C'
        plus other previously gathered data
        }

    141:{
        'Lev3': u'452', 
        'Marker': u'188', 
        'TYPE': u'141', 
        'Location': u'B'
        plus other previously gathered data
        }
    142:etc
    ...
}
Cory Kramer

You can update with item instead of data

for item in data:
    parts[item['TYPE']].update(item)

Result

>>> parts
{'141': {'Lev3': '452', 'TYPE': '141', 'Marker': '188', 'Location': 'B'},
 '140': {'Lev2': '652', 'TYPE': '140', 'Marker': '1', 'Location': 'C'},
 '139': {'Lev1': '82', 'Marker': '16', 'Location': 'A', 'TYPE': '139'}}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

python - iterating list of dictionaries and unpacking

From Java

python filter list of dictionaries based on key value

From Dev

python merge list of dictionaries based on key

From Dev

Filter a list of dictionaries in Python based on key

From Dev

Sorting a list of dictionaries based on a key

From Dev

How to categorize list of dictionaries based on the value of a key in python efficiently?

From Dev

Merging a list of dictionaries in python based on one key/value pair?

From Dev

How to categorize list of dictionaries based on the value of a key in python efficiently?

From Dev

Performance implications of unpacking dictionaries in Python

From Dev

python: unpacking an array of dictionaries in a matrix

From Java

Intersection of two list of dictionaries based on a key

From Dev

inside list of dictionaries, merge lists based on key

From Dev

Intersection of two list of dictionaries based on a key

From Dev

Summarize a list of dictionaries based on common key values

From Dev

Sort python list of dictionaries by key if key exists

From Dev

Update a value in a list of dictionaries in python based on a condition based on the value of another key in that dictionary

From Dev

Python - Filter list of dictionaries based on if key value contains all items in another list

From Dev

remove dictionaries from list based on key value from nested dictionary in python

From Dev

python unpacking list of tuples

From Dev

Unpacking big list in python

From Dev

Unpacking big list in python

From Dev

Python deduplicate list of Dictionaries by Value of a Key

From Dev

python check if dictionaries value of a key duplicate in list

From Dev

Python - Convert List Of Dictionaries & Key Values To String

From Dev

python check if dictionaries value of a key duplicate in list

From Dev

Updating values from specific key in list of dictionaries based on a separate list

From Dev

Python:Counting the number of dictionaries in which a key occur in a list of dictionaries

From Dev

Python 3 - Search list of dictionaries based on string

From Dev

Python Dictionaries: Grouping Key, Value pairs based on a common key, value

Related Related

  1. 1

    python - iterating list of dictionaries and unpacking

  2. 2

    python filter list of dictionaries based on key value

  3. 3

    python merge list of dictionaries based on key

  4. 4

    Filter a list of dictionaries in Python based on key

  5. 5

    Sorting a list of dictionaries based on a key

  6. 6

    How to categorize list of dictionaries based on the value of a key in python efficiently?

  7. 7

    Merging a list of dictionaries in python based on one key/value pair?

  8. 8

    How to categorize list of dictionaries based on the value of a key in python efficiently?

  9. 9

    Performance implications of unpacking dictionaries in Python

  10. 10

    python: unpacking an array of dictionaries in a matrix

  11. 11

    Intersection of two list of dictionaries based on a key

  12. 12

    inside list of dictionaries, merge lists based on key

  13. 13

    Intersection of two list of dictionaries based on a key

  14. 14

    Summarize a list of dictionaries based on common key values

  15. 15

    Sort python list of dictionaries by key if key exists

  16. 16

    Update a value in a list of dictionaries in python based on a condition based on the value of another key in that dictionary

  17. 17

    Python - Filter list of dictionaries based on if key value contains all items in another list

  18. 18

    remove dictionaries from list based on key value from nested dictionary in python

  19. 19

    python unpacking list of tuples

  20. 20

    Unpacking big list in python

  21. 21

    Unpacking big list in python

  22. 22

    Python deduplicate list of Dictionaries by Value of a Key

  23. 23

    python check if dictionaries value of a key duplicate in list

  24. 24

    Python - Convert List Of Dictionaries & Key Values To String

  25. 25

    python check if dictionaries value of a key duplicate in list

  26. 26

    Updating values from specific key in list of dictionaries based on a separate list

  27. 27

    Python:Counting the number of dictionaries in which a key occur in a list of dictionaries

  28. 28

    Python 3 - Search list of dictionaries based on string

  29. 29

    Python Dictionaries: Grouping Key, Value pairs based on a common key, value

HotTag

Archive