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.
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)
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)
{‘KEY1’: ‘hello’, ‘Key2’: ‘world’}
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)
{‘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.
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