I have a list of dictionaries. How would I go about converting all dictionaries' values to lowercase, whenever possible?
One caveat is that values can be None's, as well as ints.
Here's an example for what I have:
list_of_dicts = [{'People': 3, 'Sport': None},{'People': 6, 'Sport': 'Football'} ]
Expected result (note the last value - Football - has been lowercased:
list_of_dicts = [{'People': 3, 'Sport': None},{'People': 6, 'Sport': 'football'} ]
The accepted solution below works great. However, if you want only values for specific keys modified, you can tack on a quick test like so:
list_of_dicts = [{'PeopleCount': 3, 'Sport': None}, {'PeopleCount': 6, 'Sport': 'Football'} ]
for d in list_of_dicts:
for key in d.keys():
if isinstance(d[key], str) and key in ('sport','zzz','Sport'): #test if this is the field we want to lowercase
d[key] = d[key].lower()
print(list_of_dicts)