Reputation: 317
I know that this is simple, but I could not figure it out. I want to convert all values on a list of dictionaries that look like the one bellow to lowercase:
{'John greased ': ['Axle', 'wheel', 'wheels', 'wheel', 'enGine', ''],
'Maria testa': ['teste', 'teste', '', '', '', ''],
'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
'Lisa plowed ': ['field', 'Field', 'FIELD', 'bola', '', '']}
I tried using:
low = {k.lower():v.lower() for k, v in result.items()}
print(low)
But it won't work. Any suggestions on how to fix that? Thank you very much!
Upvotes: 1
Views: 2101
Reputation: 1121484
Your values are lists, so add a list comprehension:
{k.lower(): [i.lower() for i in v] for k, v in result.items()}
Demo:
>>> result = {'John greased ': ['Axle', 'wheel', 'wheels', 'wheel', 'enGine', ''],
... 'Maria testa': ['teste', 'teste', '', '', '', ''],
... 'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
... 'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
... 'Lisa plowed ': ['field', 'Field', 'FIELD', 'bola', '', '']}
>>> {k.lower(): [i.lower() for i in v] for k, v in result.items()}
{'lisa plowed ': ['field', 'field', 'field', 'bola', '', ''], 'tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'], 'paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''], 'maria testa': ['teste', 'teste', '', '', '', ''], 'john greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', '']}
>>> from pprint import pprint
>>> pprint({k.lower(): [i.lower() for i in v] for k, v in result.items()})
{'john greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', ''],
'lisa plowed ': ['field', 'field', 'field', 'bola', '', ''],
'maria testa': ['teste', 'teste', '', '', '', ''],
'paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
'tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza']}
Upvotes: 5