python
Convert all dictionary keys to uppercase in python
You can use one of these methods to convert all the dictionary keys to uppercase format in Python.
# Define a dictionary
car = {
"Modal": "X1s",
"Showroom Address": "India",
"Brand": "TATA",
"Car Type": "Petrol"
}
# Convert dictionary keys to lowercase
car = {key.upper(): val for key, val in car.items()}
# Print the result
print(car)
Output
{'MODAL': 'X1s', 'SHOWROOM ADDRESS': 'India', 'BRAND': 'TATA', 'CAR TYPE': 'Petrol'}
We are using the upper() function of Python String along with dict comprehension to convert all the dictionary keys to UPPERCASE format in Python.
If you do not want to use dict comprehension then you can use the below code.
car = {
"Modal": "X1s",
"Showroom Address": "India",
"Brand": "TATA",
"Car Type": "Petrol"
}
result = {}
for key, value in car.items():
result[key.upper()] = value
print(result)
Output
{'MODAL': 'X1s', 'SHOWROOM ADDRESS': 'India', 'BRAND': 'TATA', 'CAR TYPE': 'Petrol'}
Was this helpful?
Similar Posts
- Convert all dictionary keys to lowercase in python
- Get all keys from a dictionary in Python
- [Python] Using comprehension expression to convert list of dictionaries to nested dictionary
- Convert pandas DataFrame to python collection - dictionary
- Convert a dictionary to pandas DataFrame in python
- Show all files in a dictionary as a list in Python
- Convert a Python Dictionary to Pandas DataFrame