avatar Bite 329. Convert dict keys to snake case

In this Bite we will take a (nested) dict and transform its (string) keys to snake case.

So for example this dictionary:

{
    "camelCase": "Value1",
    "camelcase": "Value1",
    "PascalCase": "Value2",
    "kebab-case": "Value3",
    "ACRONYM": "Value4",
    "number22": "Value5"
}

Would result in the following dictionary where the keys have been converted into snake_case:

{
    "camel_case": "Value1",
    "camelcase": "Value1",
    "pascal_case": "Value2",
    "kebab_case": "Value3",
    "a_c_r_o_n_y_m": "Value4",
    "number_22": "Value5"
}

As you can see from this example hyphens, numbers and acronyms trigger snake casing as well.

Additionally, it should work with nested data structures as well. For example a dict of dicts and a dict where the values contain a (nested) list.

Login and get coding
go back Advanced level
Bitecoin 4X

46 out of 46 users completed this Bite.
Will you be the 47th person to crack this Bite?
Resolution time: ~67 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 6.5 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help