
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- How to insert new keys/values into Python dictionary?
- How to create Python dictionary from list of keys and values?
- How to split Python dictionary into multiple keys, dividing the values equally?
- Python – Limit the values to keys in a Dictionary List
- How to convert Javascript dictionary to Python dictionary?
- 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?
- Convert key-values list to flat dictionary in Python
- Find keys with duplicate values in dictionary in Python
- Python - Combine two dictionary adding values for common keys
- How to add new keys to a dictionary in Python?
- How to sort a dictionary in Python by keys?
- How to Common keys in list and dictionary using Python
- Convert string dictionary to dictionary in Python

Advertisements