Using Lower() in a list of lists in Python

Asked

Viewed 1,488 times

13

If I have such a list:

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

and I want to leave her like this:

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

How do I do it? I thought if I did just that:

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

but it didn’t work out...

3 answers

13


I used the example of that answer where it is used comprehensilist on :

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

See on 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)

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

See on Ideone

  • To list comprehensionwho owns the print() does not return the output shown, and if it is assigned to a list variable, it always returns [ None, None, None ].

  • @Lacobus accepted that we had overcome this misconception : https://ideone.com/2Nzf0Q I hope at the end of that I have conquered your +1, I will be very happy, because I like your observations and approaches.

  • I’m talking about this one: https://ideone.com/6M5BBp

  • What about this one? @Lacobus

  • It is not the same output that you presented in your answer. Both have different outputs.

  • For example: https://ideone.com/z8gt6k

  • See, my answer contains 2 examples, in 1a list comprehension, results in a list created from lista, however what is being printed is not the new list created but the items, in the 2nd example I assign the new list created under the name of lista_lower and then madno lista_lower for the exit exit... To understand the result of your example (https://ideone.com/z8gt6k), it is necessary to perform a table test : https://answall.com/questions/220474/o-que-%C3%A9-a-table-test-as-applic%C3%A1-lo ... It’s on the chopping block?

  • What happens is that print() returns None always. As the entry list contains 3 nested lists, print() is called 3 times, resulting in a list containing 3 elements None.

  • @Lacobus What exactly is the point in question you refer to? What are you trying to say? In the example in which I use the print it returns the desired values... Then I assign to a variable that could be the same name as the original list... Does my answer contain any errors? I am being patient, to try to understand you, but in fact, you are not being understandable... Which part of the answer does not please you, as I said the first output is the result of the loop print is in the loop, to print the list you should play the print out of the loop in the list...

Show 4 more comments

9

A way to use the Lower in this list list, is to use the function map() and give an expression lambda for her lambda x:x.lower(), who had used the function lower() to do the job.

However, it is necessary to join the lists in a list only with the function chain() module itertools and getting a temporary list with the function list(), and then get the return in the format of a single list using the function again list() to generate your new list. Otherwise the function map() will issue 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)

Exit:

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

See the code working on repl it..

Editing

Not specified in question you want to keep the list structure. If you want maintain the structure of the list, just do the interaction on a for to get the sub-lists and then add each sub-list in your new list.

Look how it turned out:

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)

Exit:

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

No longer needed to use module itertools in this new version of the code.

See the new version working on repl it..

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

  • 1

    Remembering that the list was "flattened" during the conversion and lost the original dimensions.

  • 1

    Exactly... the list structure has been lost.

  • @Williamhenrique you did not leave explicit in the question that would want to maintain the structure, because the focus was to use the function lower() to convert the characters. I then edit the answer and put it in the structure mentioned.

  • Is there anything else I can do? I didn’t understand why the negative.

  • @cat I left explicitly yes in question... look at the line that says EXIT. The negative reason I already took.. I must have clicked on the down arrow and I didn’t even notice! Anyway thank you help!! All help is welcome! :)

  • All right @Williamhenrique

Show 1 more comment

6

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

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) )

Exit:

[['de' 'do' 'ou']
 ['ae' 'yhh' 'oo']
 ['ow' 'la' 'for']
 ['x' 'k' 'n']]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.