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

Thijs Cobben

Given a list of dictionaries:

the_list = [{k1:v1,k2:v2}, {k1:v3, k3:v4}, {k1:v5,k2:v6},{k3:v7}]

How can I update the value for k2 based on a condition based on the value of k1?

I would come up with this solution but it doesn't feel Pythonic. Better solutions anyone?

for item in the_list:
  if set([k1,k2]) <= item.keys():
    #cond evaluates to True or False
    if cond(item[k1]):
      item.update({k2:newvalue})

Maybe this can be done better with a map or lambda expression?

Kshitij Saraogi

This is the simplest code I came up with to perform the same operation:

my_list = [{k1:v1,k2:v2}, {k1:v3, k3:v4}, {k1:v5,k2:v6},{k3:v7}]
for my_dict in my_list:
    if all (k in my_dict for k in ("k1", "k2")):
        if cond(my_dict[k1]):
            my_dict[k2] = newvalue # possibly pre-determined 

Depending on the way cond evaluates the value of k1 and/or newvalue is generated, the code might be further more "Pythonized".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

python filter list of dictionaries based on key value

From Dev

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

From Dev

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

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

Python - Sum the value in the list of dictionary based on the same key

From Dev

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

From Dev

fetch key based on value from python dictionary

From Dev

Order a list of dictionaries based on another dictionary

From Dev

Python: How to group dictionaries based on a key value pair

From Dev

find key value based on value field python dictionary

From Dev

Update key value dictionary list?

From Dev

find and update a value of a dictionary in list of dictionaries

From Dev

find and update a value of a dictionary in list of dictionaries

From Dev

Replacing the value of a Python dictionary with the value of another dictionary that matches the other dictionaries key

From Dev

How to append key value pair to an existing list of dict from another list of dict based on matching Key in Python

From Dev

Python Remove duplicates from list of dictionaries based on a value

From Dev

Python Remove duplicates from list of dictionaries based on a value

From Dev

Increment value in the list based on condition

From Dev

javascript update object key/value based on another object by mapping

From Dev

Python Dictionary contains List as Value - How to update based on some other input

From Dev

Unpacking a list of dictionaries based on key matches in Python

From Dev

python merge list of dictionaries based on key

From Dev

Filter a list of dictionaries in Python based on key

From Dev

Get dictionaries in a list where the key:value pairs are in another list of dictionaries

From Dev

Get dictionaries in a list where the key:value pairs are in another list of dictionaries

From Dev

Update python dictionary (add another value to existing key)

From Dev

Outputting a sorted list in order based on dictionaries value

Related Related

  1. 1

    python filter list of dictionaries based on key value

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

    Python - Sum the value in the list of dictionary based on the same key

  8. 8

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

  9. 9

    fetch key based on value from python dictionary

  10. 10

    Order a list of dictionaries based on another dictionary

  11. 11

    Python: How to group dictionaries based on a key value pair

  12. 12

    find key value based on value field python dictionary

  13. 13

    Update key value dictionary list?

  14. 14

    find and update a value of a dictionary in list of dictionaries

  15. 15

    find and update a value of a dictionary in list of dictionaries

  16. 16

    Replacing the value of a Python dictionary with the value of another dictionary that matches the other dictionaries key

  17. 17

    How to append key value pair to an existing list of dict from another list of dict based on matching Key in Python

  18. 18

    Python Remove duplicates from list of dictionaries based on a value

  19. 19

    Python Remove duplicates from list of dictionaries based on a value

  20. 20

    Increment value in the list based on condition

  21. 21

    javascript update object key/value based on another object by mapping

  22. 22

    Python Dictionary contains List as Value - How to update based on some other input

  23. 23

    Unpacking a list of dictionaries based on key matches in Python

  24. 24

    python merge list of dictionaries based on key

  25. 25

    Filter a list of dictionaries in Python based on key

  26. 26

    Get dictionaries in a list where the key:value pairs are in another list of dictionaries

  27. 27

    Get dictionaries in a list where the key:value pairs are in another list of dictionaries

  28. 28

    Update python dictionary (add another value to existing key)

  29. 29

    Outputting a sorted list in order based on dictionaries value

HotTag

Archive