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 a dictionary into lower case: Python.Here ( Data Science Learner ) is some code tweaks with output and sample python code.

1. Convert all keys of the dictionary into lower case –

Here I will convert all the keys of the dictionary into lower case using the lower() function.

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 the dictionary into lower case1

2. Convert all values of the dictionary into lower case –

Using the same lower() function you can convert values 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 the keys and values of the dictionary into the upper case?

In the same way, If you need to change the case of a dictionary to upper case, Use the upper() function in the place of lower() function. 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’}

Python Dictionary is an inbuilt data structure in Python. We widely use this data structure in so many practical scenarios. Read more about python dict here.

dict python
dict python

Conclusion –

Dictionary is one of the most rapidly used data structures in python. In General, We need to compare the keys and values of dictionaries with some variables. This creates a huge need for the above operations. The upper() and lower() function convert the string into different cases on the basis of their name. Both functions are system-defined 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 keeping notes for such tricks.

If you have anything to add to this topic, please comment below in the comment box. I hope this article must have solved your issue. If you are still struggling, please let us know.

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.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner