Liam Emery Liam Emery - 2 years ago 107
Python Question

How to convert lists inside lists to lowercase?

So I have a list

["Could you be quiet?","Okay I will be quiet"]]

I would like to know how I can convert every letter to lowercase so that I can run a search against the list to find common words, and then print it back out in original form. For example:

some code to convert list to lowercase
some code to search for a term
some code to convert list to original form
some code to print all matches

Because the code I have tried, only works for lists that don't contain more lists inside of them.!

Answer Source

Use list comprehension to create a separate list with the strings in lower case

>>> [[j.lower() for j in i] for i in myList1]
[['hello how are you', 'im good thanks'], ['could you be quiet?', 'okay i will be quiet']]

This won't modify myList1.

EDIT: According to the OP's question, this is not a feasible solution since OP wishes to compare the lowercase list and then return the result strings in it's original case. Hence, when searching the list, OP should simply convert each string to lowercase using .lower() and then compare it with the search query. No need for a separate list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download