More pythonic way to change multilevel dictionary values

Casimir Crystal

For example, I have the following dict:

{'foo': 'test', 
 'bar': {'test1': 'some text here'},
 'baz': {'test2': {'test3': 'some text here', 'test4': 'some text here'}}}

Or something like this, maybe more levels.

Now the question is, I want to change...'some text here' to 'A test'. Yeah, I can use a lot of for loops like the following code:

d = {'foo': 'test',
     'bar': {'test1': 'some text here'},
     'baz': {'test2': {'test3': 'some text here',
                       'test4': 'some text here'}}}

for i in d:
    if d[i] == 'some text here':
        d[i] = 'A test'

    elif type(d[i]) == dict:
        for j in d[i]:
            if d[i][j] == 'some text here':
                d[i][j] = 'A test'

            elif type(d[i][j]) == dict:
                for n in d[i][j]:
                    if d[i][j][n] == 'some text here':
                        d[i][j][n] = 'A test'

__import__('pprint').pprint(d)

Output:

{'bar': {'test1': 'A test'},                                                    
 'baz': {'test2': {'test3': 'A test', 'test4': 'A test'}},
 'foo': 'test'}

However I don't think this is a good way...Any ideas?

Dima Tisnek

As long as values you are changing are not too hairy, here's a one-liner:

In [29]: data = dict(foo=12, bar=dict(dd=12), ex=dict(a=dict(b=dict(v=12))))

In [30]: json.loads(re.sub("12", "33", json.dumps(data)))
Out[30]: {'bar': {'dd': 33}, 'ex': {'a': {'b': {'v': 33}}}, 'foo': 33}

EDIT, per Kevin, simple substitution doesn't even need re:

json.loads(json.dumps(data).replace("12", "33"))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pythonic way of setting values in a dictionary

From Dev

pythonic way of reassigning values to dictionary

From Dev

Is there a more pythonic/compact way to create this dictionary?

From Dev

More pythonic way of updating an existing value in a dictionary

From Dev

Change List of Tuples into Dictionary (pythonic way)

From Dev

Change Column Data Type in More Pythonic Way

From Dev

Pythonic way to populate a dictionary with count of values

From Python

Pythonic way to replace values in a list according to a dictionary

From Dev

Pythonic way to check empty dictionary and empty values

From Dev

Is there a more Pythonic way to iterate through dictionary keys looking for a value than this?

From Dev

What's a more Pythonic way of grabbing N items from a dictionary?

From Dev

Replacing values in a list of lists with dictionary values, pythonic or better way

From Dev

A Pythonic way to "query" a dictionary

From Dev

Is there a more pythonic way to write

From Dev

A more pythonic (or pandorable) way to change a list of columns to different data types

From Dev

pythonic way of calling specific keys/values from a nest dictionary

From Dev

Is there a Pythonic way to extend one dictionary with another (preserving all values)

From Dev

What is the most pythonic way to copy values from one dictionary into another?

From Dev

Pythonic way of counting max elements by index in a dictionary with list values

From Dev

Pythonic way to create a dictionary from array based index values

From Dev

Pythonic way to group a list using a dictionary that has lists as values

From Dev

Pythonic way to merge keys with common values for a single dictionary

From Dev

Better / More pythonic way to process the results of a function with multiple return values

From Dev

More pythonic way of filtering out stable values in a list

From Dev

More elegant way of incrementing values in dictionary

From Dev

Python Dictionary Insert multilevel values

From Dev

Is there a more pythonic/more efficient way to loop through dictionary containing lists rather than using for loops?

From Dev

Is there a Pythonic way of adding float values of one dictionary to a list of values in another dictionary?

From Dev

Pythonic Way to Change Series Values for Index Elements in Series and a Separate Dataframe

Related Related

  1. 1

    pythonic way of setting values in a dictionary

  2. 2

    pythonic way of reassigning values to dictionary

  3. 3

    Is there a more pythonic/compact way to create this dictionary?

  4. 4

    More pythonic way of updating an existing value in a dictionary

  5. 5

    Change List of Tuples into Dictionary (pythonic way)

  6. 6

    Change Column Data Type in More Pythonic Way

  7. 7

    Pythonic way to populate a dictionary with count of values

  8. 8

    Pythonic way to replace values in a list according to a dictionary

  9. 9

    Pythonic way to check empty dictionary and empty values

  10. 10

    Is there a more Pythonic way to iterate through dictionary keys looking for a value than this?

  11. 11

    What's a more Pythonic way of grabbing N items from a dictionary?

  12. 12

    Replacing values in a list of lists with dictionary values, pythonic or better way

  13. 13

    A Pythonic way to "query" a dictionary

  14. 14

    Is there a more pythonic way to write

  15. 15

    A more pythonic (or pandorable) way to change a list of columns to different data types

  16. 16

    pythonic way of calling specific keys/values from a nest dictionary

  17. 17

    Is there a Pythonic way to extend one dictionary with another (preserving all values)

  18. 18

    What is the most pythonic way to copy values from one dictionary into another?

  19. 19

    Pythonic way of counting max elements by index in a dictionary with list values

  20. 20

    Pythonic way to create a dictionary from array based index values

  21. 21

    Pythonic way to group a list using a dictionary that has lists as values

  22. 22

    Pythonic way to merge keys with common values for a single dictionary

  23. 23

    Better / More pythonic way to process the results of a function with multiple return values

  24. 24

    More pythonic way of filtering out stable values in a list

  25. 25

    More elegant way of incrementing values in dictionary

  26. 26

    Python Dictionary Insert multilevel values

  27. 27

    Is there a more pythonic/more efficient way to loop through dictionary containing lists rather than using for loops?

  28. 28

    Is there a Pythonic way of adding float values of one dictionary to a list of values in another dictionary?

  29. 29

    Pythonic Way to Change Series Values for Index Elements in Series and a Separate Dataframe

HotTag

Archive