0

I'm trying to do a programmatic modification of dicts. After floundering on my own, I tried lots of different prompts at ChatGPT and Bard, but they didn't generate anything the gets my desired output.

I think I have used glom for this kind of thing in the past, but can't find any of my snippets to build off of.

There are some similar questions on SO, but mine is different in that I want to modify scalar values, or each of the elements in a list value.

I have dicts like this:

{
  "name": ["joe", "bob", "jane"],
  "rank": "beginner",
  "address": {
    "street": {"direction": "east", "name": "main"},
    "town": {"name": "boston", "state": "mass"},
    "name": {"first": "joe", "last": "smith"},
  },
  "clubs": {
    "art": {"name": "art club", "rank": "expert", "site": "art.com"},
  },
  "fake": {
    "real": {"name": "fake"},
    "fake": {"name": "fake"},
  },
}

I'm trying to write a function that would recurse through the dict and apply a function to all scalar and list values of the keys in a list I specify.

For example, if my list was ["name", "rank"] and the function was string capitalization, then I would expect the output to be

{
  "name": ["JOE", "BOB", "JANE"],
  "rank": "BEGINNER",
  "address": {"street": {"direction": "east", "name": "MAIN"}},
  "clubs": {"art": {"name": "ART CLUB", "rank": "EXPERT", "site": "art.com"}},
  "fake": {"real": {"name": "FAKE"}, "fake": {"name": "FAKE"}},
}
Mark Miller
  • 3,011
  • 1
  • 14
  • 34
  • 1
    Instead of using story-writing services (which is what chatgpt and bard are, they are *explicitly* not educational tools or codewriting services, they write _stories_ and lie as much as they need to in order for those stories to seem credible), use Google or DuckDuckGo or any other real search engine. E.g. "deep replace all values for a key in a dict" or something. The *first* result I found was the Stackoverflow post [How to recursively replace dictionary values with a matching key?](https://stackoverflow.com/questions/45335445/how-to-recursively-replace-dictionary-values-with-a-matching-key) – Mike 'Pomax' Kamermans Aug 19 '23 at 20:58
  • Thanks guys. There's at least [one answer](https://stackoverflow.com/a/73905000/3860847) in there that I could have built off of. – Mark Miller Aug 19 '23 at 21:26

1 Answers1

1

If dct is your dictionary from the question, you can do:

def change(o, keys_to_capitalize, should_capitalize=False):
    if isinstance(o, dict):
        return {
            k: change(
                v, keys_to_capitalize, k in keys_to_capitalize
            )
            for k, v in o.items()
        }
    elif isinstance(o, list):
        return [change(v, keys_to_capitalize, should_capitalize) for v in o]
    else:
        return o.upper() if should_capitalize else o


dct = change(dct, {"name", "rank"})
print(dct)

Prints:

{
    "name": ["JOE", "BOB", "JANE"],
    "rank": "BEGINNER",
    "address": {
        "street": {"direction": "east", "name": "MAIN"},
        "town": {"name": "BOSTON", "state": "mass"},
        "name": {"first": "joe", "last": "smith"},
    },
    "clubs": {"art": {"name": "ART CLUB", "rank": "EXPERT", "site": "art.com"}},
    "fake": {"real": {"name": "FAKE"}, "fake": {"fake": {"name": "FAKE"}}},
}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91