

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert Python dictionary keys/values to lowercase?
You can convert Python dictionary keys/values to lowercase by simply iterating over them and creating a new dict from the keys and values. For example,
def lower_dict(d): new_dict = dict((k.lower(), v.lower()) for k, v in d.items()) return new_dict a = {'Foo': "Hello", 'Bar': "World"} print(lower_dict(a))
This will give the output
{'foo': 'hello', 'bar': 'world'}
If you want just the keys to be lower cased, you can call lower on just that. For example,
def lower_dict(d): new_dict = dict((k.lower(), v) for k, v in d.items()) return new_dict a = {'Foo': "Hello", 'Bar': "World"} print(lower_dict(a))
This will give the output
{'foo': 'Hello', 'bar': 'World'}
- Related Questions & Answers
- How to insert new keys/values into Python dictionary?
- How to create Python dictionary from list of keys and values?
- How to convert Javascript dictionary to Python dictionary?
- How to split Python dictionary into multiple keys, dividing the values equally?
- Keys associated with Values in Dictionary in Python
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- How to create Python dictionary with duplicate keys?
- Python – Limit the values to keys in a Dictionary List
- Find keys with duplicate values in dictionary in Python
- Python - Combine two dictionary adding values for common keys
- Convert key-values list to flat dictionary in Python
- How to add new keys to a dictionary in Python?
- How to sort a dictionary in Python by keys?
- Convert string dictionary to dictionary in Python
- How does Python dictionary keys() Method work?
Advertisements