Dictionary to lowercase in Python

PythonDictionaryLowercase

Python Problem Overview


I wish to do this but for a dictionary:

"My string".lower()

Is there a built in function or should I use a loop?

Python Solutions


Solution 1 - Python

You will need to use either a loop or a list/generator comprehension. If you want to lowercase all the keys and values, you can do this::

dict((k.lower(), v.lower()) for k,v in {'My Key':'My Value'}.iteritems())

If you want to lowercase just the keys, you can do this::

dict((k.lower(), v) for k,v in {'My Key':'My Value'}.iteritems())

Generator expressions (used above) are often useful in building dictionaries; I use them all the time. All the expressivity of a loop comprehension with none of the memory overhead.

Solution 2 - Python

The following is identical to Rick Copeland's answer, just written without a using generator expression:

outdict = {}
for k, v in {'My Key': 'My Value'}.iteritems():
    outdict[k.lower()] = v.lower()

Generator-expressions, list comprehension's and (in Python 2.7 and higher) dict comprehension's are basically ways of rewriting loops.

In Python 2.7+, you can use a dictionary comprehension (it's a single line of code, but you can reformat them to make it more readable):

{k.lower():v.lower()
    for k, v in
    {'My Key': 'My Value'}.items()
}

They are quite often tidier than the loop equivalent, as you don't have to initialise an empty dict/list/etc.. but, if you need to do anything more than a single function/method call they can quickly become messy.

Solution 3 - Python

If you want keys and values of multi-nested dictionary (json format) lowercase, this might help. Need to have support for dict comprehensions what should be in Python 2.7

dic = {'A':['XX', 'YY', 'ZZ'],
       'B':(u'X', u'Y', u'Z'),
       'C':{'D':10,
            'E':('X', 'Y', 'Z'),
            'F':{'X', 'Y', 'Z'}
           },
       'G':{'X', 'Y', 'Z'}
      }

PYTHON2.7 -- also supports OrderedDict

def _lowercase(obj):
    """ Make dictionary lowercase """
    if isinstance(obj, dict):
        t = type(obj)()
        for k, v in obj.items():
            t[k.lower()] = _lowercase(v)
        return t
    elif isinstance(obj, (list, set, tuple)):
        t = type(obj)
        return t(_lowercase(o) for o in obj)
    elif isinstance(obj, basestring):
        return obj.lower()
    else:
        return obj 

PYTHON 3.6

def _lowercase(obj):
    """ Make dictionary lowercase """
    if isinstance(obj, dict):
        return {k.lower():_lowercase(v) for k, v in obj.items()}
    elif isinstance(obj, (list, set, tuple)):
        t = type(obj)
        return t(_lowercase(o) for o in obj)
    elif isinstance(obj, str):
        return obj.lower()
    else:
        return obj

Solution 4 - Python

Shorter way in python 3: {k.lower(): v for k, v in my_dict.items()}

Solution 5 - Python

This will lowercase all your dict keys. Even if you have nested dict or lists. You can do something similar to apply other transformations.

def lowercase_keys(obj):
  if isinstance(obj, dict):
    obj = {key.lower(): value for key, value in obj.items()}
    for key, value in obj.items():         
      if isinstance(value, list):
        for idx, item in enumerate(value):
          value[idx] = lowercase_keys(item)
      obj[key] = lowercase_keys(value)
  return obj 
json_str = {"FOO": "BAR", "BAR": 123, "EMB_LIST": [{"FOO": "bar", "Bar": 123}, {"FOO": "bar", "Bar": 123}], "EMB_DICT": {"FOO": "BAR", "BAR": 123, "EMB_LIST": [{"FOO": "bar", "Bar": 123}, {"FOO": "bar", "Bar": 123}]}}

lowercase_keys(json_str)


Out[0]: {'foo': 'BAR',
 'bar': 123,
 'emb_list': [{'foo': 'bar', 'bar': 123}, {'foo': 'bar', 'bar': 123}],
 'emb_dict': {'foo': 'BAR',
  'bar': 123,
  'emb_list': [{'foo': 'bar', 'bar': 123}, {'foo': 'bar', 'bar': 123}]}}

Solution 6 - Python

I used JSON library to deserialize the dictionary first, apply lower case than convert back to dictionary

import json
mydict = {'UPPERCASE': 'camelValue'}
print(mydict)
mydict_in_str = json.dumps(mydict)
mydict_lowercase = json.loads(mydict_in_str.lower())
print(mydict_lowercase)

Solution 7 - Python

In Python 3:

d = dict()
d = {k.casefold(): v for k, v in d.items()}

Solution 8 - Python

If the dictionary supplied have multiple type of key/values(numeric, string etc.); then use the following solution.

For example; if you have a dictionary named mydict as shown below

mydict = {"FName":"John","LName":"Doe",007:true}

In Python 2.x

dict((k.lower() if isinstance(k, basestring) else k, v.lower() if isinstance(v, basestring) else v) for k,v in mydict.iteritems())

In Python 3.x

dict((k.lower() if isinstance(k, str) else k, v.lower() if isinstance(v, str) else v) for k,v in mydict.iteritems())

Note: this works good on single dimensional dictionaries

Solution 9 - Python

Love the way you can use multilevel functions, here's my way of lowercase-ing the keys

def to_lower(dictionary):

    def try_iterate(k):
        return lower_by_level(k) if isinstance(k, dict) else k

    def try_lower(k):
        return k.lower() if isinstance(k, str) else k

    def lower_by_level(data):
        return dict((try_lower(k), try_iterate(v)) for k, v in data.items())

    return lower_by_level(dictionary)

Solution 10 - Python

I am answering this late - as the question is tagged Python. Hence answering with a solution for both Python 2.x and Python 3.x, and also handling the case of non-string keys.

Python 2.x - using dictionary comprehension

{k.lower() if isinstance(k, basestring) else k: v.lower() if isinstance(v, basestring) else v for k,v in yourDict.iteritems()}

Demo:

>>> yourDict = {"Domain":"WORKGROUP", "Name": "CA-LTP-JOHN", 111: 'OK', "Is_ADServer": 0, "Is_ConnectionBroker": 0, "Domain_Pingable": 0}
>>> {k.lower() if isinstance(k, basestring) else k: v.lower() if isinstance(v, basestring) else v for k,v in yourDict.iteritems()}
{'domain': 'workgroup', 'name': 'ca-ltp-john', 'is_adserver': 0, 'is_connectionbroker': 0, 111: 'ok', 'domain_pingable': 0}


Python 3.x - no iteritems() in Python 3.x

{k.lower() if isinstance(k, str) else k: v.lower() if isinstance(v, str) else v for k,v in yourDict.items()}

Demo:

>>> dict((k.lower() if isinstance(k, basestring) else k, v.lower() if isinstance(v, basestring) else v) for k,v in yourDict.iteritems())
Traceback (most recent call last):
  File "python", line 1, in <module>
AttributeError: 'dict' object has no attribute 'iteritems'
>>> {k.lower() if isinstance(k, str) else k: v.lower() if isinstance(v, str) else v for k,v in yourDict.items()}
>>> {'domain': 'workgroup', 'name': 'ca-ltp-john', 111: 'ok', 'is_adserver': 0, 'is_connectionbroker': 0, 'domain_pingable': 0}

Solution 11 - Python

In Python 3 you can do:

dict((k.lower(), v.lower()) for k,v in {'Key':'Value'}.items())

In Python 2 just substitute .items() for .iteritems():

dict((k.lower(), v.lower()) for k,v in {'Key':'Value'}.iteritems())

Solution 12 - Python

def convert_to_lower_case(data):
    if type(data) is dict:
        for k, v in data.iteritems():
            if type(v) is str:
                data[k] = v.lower()
            elif type(v) is list:
                data[k] = [x.lower() for x in v]
            elif type(v) is dict:
                data[k] = convert_to_lower_case(v)
    return data

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTeifionView Question on Stackoverflow
Solution 1 - PythonRick CopelandView Answer on Stackoverflow
Solution 2 - PythondbrView Answer on Stackoverflow
Solution 3 - PythonvldbncView Answer on Stackoverflow
Solution 4 - PythondamioView Answer on Stackoverflow
Solution 5 - PythonAndré SionekView Answer on Stackoverflow
Solution 6 - PythonzebView Answer on Stackoverflow
Solution 7 - PythonJoe HefferView Answer on Stackoverflow
Solution 8 - PythonFinny AbrahamView Answer on Stackoverflow
Solution 9 - PythonDecebalView Answer on Stackoverflow
Solution 10 - PythonNabeel AhmedView Answer on Stackoverflow
Solution 11 - PythonvasistaView Answer on Stackoverflow
Solution 12 - PythonsreeView Answer on Stackoverflow