{"id":345,"date":"2014-11-20T17:03:03","date_gmt":"2014-11-20T22:03:03","guid":{"rendered":"http:\/\/www.popmartian.com\/tipsntricks\/?p=345"},"modified":"2014-11-20T17:03:03","modified_gmt":"2014-11-20T22:03:03","slug":"how-to-lower-case-all-dictionary-keys-in-a-complex-python-dictionary","status":"publish","type":"post","link":"https:\/\/www.popmartian.com\/tipsntricks\/2014\/11\/20\/how-to-lower-case-all-dictionary-keys-in-a-complex-python-dictionary\/","title":{"rendered":"HOW TO Lower Case All Dictionary Keys in a Complex Python Dictionary"},"content":{"rendered":"<p>This tutorial shows you how to write and use a function that will iterate over complex, nested dictionary or list or combination of the two.  For any dictionaries it finds, it will rename the keys to lower-case.<\/p>\n<p>This is useful when trying to match an known string with a key name returned from another source where the characters are correct, but may come back in camelCase or in the wrong or just unknown case.<\/p>\n<p>First: Write the function.<code><br \/>\ndef&nbsp;renameKeys(iterable):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;type(iterable)&nbsp;is&nbsp;dict:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;key&nbsp;in&nbsp;iterable.keys():<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iterable[key.lower()]&nbsp;=&nbsp;iterable.pop(key)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;type(iterable[key.lower()])&nbsp;is&nbsp;dict&nbsp;or&nbsp;type(iterable[key.lower()])&nbsp;is&nbsp;list:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iterable[key.lower()]&nbsp;=&nbsp;renameKeys(iterable[key.lower()])<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;type(iterable)&nbsp;is&nbsp;list:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;item&nbsp;in&nbsp;iterable:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item&nbsp;=&nbsp;renameKeys(item)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;iterable<br \/>\n<\/code><\/p>\n<p>Next, create some sort of test list:<br \/>\n<code>somelist&nbsp;=&nbsp;[<br \/>\n&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;'myKey':&nbsp;'asdA',<br \/>\n&nbsp;&nbsp;'ASDFASDurKey':<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;[<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'QDD'&nbsp;:&nbsp;'booger'},<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'asdf',<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[1,2]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;]<br \/>\n&nbsp;&nbsp;}<br \/>\n]<br \/>\n<\/code><\/p>\n<p>Finally call it:<br \/>\n<code>somelist = renameKeys(somelist)<br \/>\n<\/code><\/p>\n<p><strong>CAUTION<\/strong>: If your dictionary has two elements with the same ascii characters in two separate cases such as mydict[&#8216;key1&#8217;] and mydict[&#8216;KEY1&#8217;], the function works on a last-match basis.  That means whichever dictionary element gets renamed last wins.<\/p>\n<blockquote><p>Did you find this post useful or have questions or comments?  Please let  me know!<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to write and use a function that will iterate over complex, nested dictionary or list or combination of the two. For any dictionaries it finds, it will rename the keys to lower-case. This is useful &hellip; <a href=\"https:\/\/www.popmartian.com\/tipsntricks\/2014\/11\/20\/how-to-lower-case-all-dictionary-keys-in-a-complex-python-dictionary\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,9,59,8],"tags":[],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-how-to-do-stuff","category-programming","category-python","category-software"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":5,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/345\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}