0

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?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    Isn't this the same question as in [Dictionary to lowercase in Python](https://stackoverflow.com/q/764235/4909087)? – cs95 Jan 02 '21 at 09:17
  • @cs95 similar, this is an array of that though. But yea, same thing. – David542 Jan 02 '21 at 09:24

2 Answers2

1

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

[{k.lower():v for k,v in item.items()} for item in d]
David542
  • 104,438
  • 178
  • 489
  • 842
0
{i.lower():j for i,j in d.items()}

where d is the dict.

Mike Smith
  • 527
  • 2
  • 6
  • 20