David542
David542

Reputation: 110083

Lowercase the keys in a dict

I have an array of dicts and I would like to lowercase the keys. Here is what I have so far:

d_lower = []
for item in d:
    item_lower = {k.lower():v for k,v in item.items()}
    d_lower.append(item_lower)

Can this be done in a single-line list-comprehension?

Upvotes: 0

Views: 66

Answers (2)

Mike Smith
Mike Smith

Reputation: 456

{i.lower():j for i,j in d.items()}

where d is the dict.

Upvotes: 0

David542
David542

Reputation: 110083

Sure, though it's not necessarily any more readable:

[{k.lower():v for k,v in item.items()} for item in d]

Upvotes: 1

Related Questions