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
To
list comprehension
who owns theprint()
does not return the output shown, and if it is assigned to a list variable, it always returns[ None, None, None ]
.– Lacobus
@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.
– MagicHat
I’m talking about this one: https://ideone.com/6M5BBp
– Lacobus
What about this one? @Lacobus
– MagicHat
It is not the same output that you presented in your answer. Both have different outputs.
– Lacobus
For example: https://ideone.com/z8gt6k
– Lacobus
See, my answer contains 2 examples, in 1a
list comprehension
, results in a list created fromlista
, 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 oflista_lower
and then madnolista_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?– MagicHat
What happens is that
print()
returnsNone
always. As the entry list contains 3 nested lists,print()
is called 3 times, resulting in a list containing 3 elementsNone
.– Lacobus
@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 loopprint
is in the loop, to print the list you should play the print out of the loop in the list...– MagicHat