python: how to make list with all lowercase?

Question:

Suppose I have some data like:

data[0] = ['I want to make everything lowercase']
data[1] = ['How Do I Do It']
data[2] = ['With A Large DataSet']

How do I repeatedly call the lower() function on all the elements of data, to make all the of strings in lowercase?

I have tried {k.lower(): v for k, v in data.items()}, but I get an error saying that 'list' object has no attribute 'items'.

I have also tried using .lower() and it is giving me the same AtrributeError.

Asked By: cohsta

||

Answers:

You need a list comprehension, not a dict comprehension:

lowercase_data = [v.lower() for v in data]
Answered By: Amber

if your list data like this:

data = ['I want to make everything lowercase', '', '']
data = [k.lower() for k in data]

if your list data is a list of string list:

data = [['I want to make everything lowercase'], ['']]
data = [[k.lower()] for l in data for k in l]

the fact is that list don’t has attribute ‘items’

Answered By: WIND.Knight