How to Convert all Keys of Dictionary into Lower Case

How to Convert all Keys of Dictionary into Lower Case ?

In case you are looking for how to convert all keys of dictionary into lower case : Python .Here ( Data Science Learner ) are some code tweaks with output and sample python code .

1. Convert all keys of dictionary into lower case –

my_dict = {'KEY1': "Hello", 'Key2': "World"} 
new_dict = dict((k.lower(), v) for k, v in my_dict .items()) 
print(new_dict

Output –

convert all keys of dictionary into lower case
all keys of dictionary into lower case1

2. Convert all values of dictionary into lower case –

my_dict = {'KEY1': "Hello", 'Key2': "World"} 
new_dict = dict((k, v.lower()) for k, v in my_dict .items()) 
print(new_dict)

Output –

{‘KEY1’: ‘hello’, ‘Key2’: ‘world’}

3.How to change keys and values of dictionary into upper case ?

In the same way , If you need to change the case of a dictionary to upper case , Use upper() function in the place of lower . For example –

my_dict = {'KEY1': "Hello", 'Key2': "World"} 
new_dict = dict((k.upper(), v.upper()) for k, v in my_dict .items()) 
print(new_dict)

Output –

{‘KEY1’: ‘HELLO’, ‘KEY2’: ‘WORLD’}

Conclusion –

Dictionary is one of the most rapidly used data structure in python . In General , We need to compare keys and values of dictionaries with some variable. This creates a huge need of the above operations . The upper() and lower() function convert the string into different cases on the basis of their name . Both functions are system define in Python Language . In case you need to know more about string operations in Python , Go for the official documentation . These small tricks in Python speed up our coding and implementation time. I strongly recommend to keep a notes for such tricks .

Thanks 

Data  Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner