Using lower () in a list of lists in Python

13

If I have a list like this:

Lista = [['DE','DO','OU'],['AE','YHH','OO'],['OW','LA','FOR']]

And I want to leave it like this:

Lista = [['de','do','ou'],['ae','yhh','oo'],['ow','la','for']]

How do I? I thought if I did it, I would do it:

for sublist in Lista:
   for itens in sublist:
      itens.lower()

But it did not work ...

    
asked by anonymous 09.10.2017 / 03:56

3 answers

13

I used the example of this answer where is used list comprehension :

lista = [['DE','DO','OU'],['AE','YHH','OO'],['OW','LA','FOR']]
[print([w.lower() for w in line]) for line in lista]

See the Ideone

Using list comprehension you create a new list or replace the previous one:

lista = [['DE','DO','OU'],['AE','YHH','OO'],['OW','LA','FOR']]

lista_lower = [[w.lower() for w in line] for line in lista]
print(lista_lower)
  

output: [['de', 'do', 'ou'], ['ae', 'yhh', 'oo'], ['ow', 'la', 'for']]

See Ideone

    
09.10.2017 / 04:07
8

One way to use lower in this list list is to use the map() function and pass a lambda expression to it lambda x:x.lower() , which used the function lower() to do the job.

However, it is necessary to join the lists in a list only with the chain() function of the module itertools and get a temporary list with the function list() , and then get the return in the format of a single list using the list() function again to generate its new list. Otherwise the map() function will give an unexpected result.

See the example code:

import itertools

Lista = [['DE','DO','OU'],['AE','YHH','OO'],['OW','LA','FOR']]

novaLista = list(map(lambda x:x.lower(), list(itertools.chain(*Lista))))

print(novaLista)

Output:

  

['de', 'do', 'ou', 'ae', 'yhh', 'oo', 'ow', 'la', 'for']

See the working code on repl.it .

Issue

It was not specified in the question that you want to keep the list structure. If you want to maintain the list structure , simply do the interaction on a for to get the sub-lists and then add each sub-list to your new list.

See how it went:

Lista = [['DE','DO','OU'],['AE','YHH','OO'],['OW','LA','FOR']]

novaLista = []

for reg in Lista:
  novaLista.append(list(map(lambda x:x.lower(), reg)))

print(novaLista)

Output:

  

[['de', 'do', 'ou'], ['ae', 'yhh', 'oo'], ['ow', 'la', 'for']]

It was no longer necessary to use the itertools module in this new version of the code.

See the new version working at repl.it .

Read more: Convert Python list with strings all to lowercase or uppercase
Making a flat list out of lists of lists in Python

    
09.10.2017 / 05:00
6

You can use the NumPy package to convert multidimensional lists without losing the original dimensions of the list:

import numpy as np

def list_lower( lst ):
    aux = np.array(lst)
    for i, v in np.ndenumerate( aux ):
        aux[i] = v.lower()
    return np.array(aux)

Lista = [['DE','DO','OU'],['AE','YHH','OO'],['OW','LA','FOR'],['X','K','N']]

print( list_lower(Lista) )

Output:

[['de' 'do' 'ou']
 ['ae' 'yhh' 'oo']
 ['ow' 'la' 'for']
 ['x' 'k' 'n']]
    
09.10.2017 / 13:43