The min function will return the smallest element in the list. However, it is necessary to consider the type of elements in the list. In the case of numbers, the smallest number.
print('Exemplo 1')
menor = min([3, 4, 2, 1])
print(menor)
Out[]: Exemplo 1
Out[]: 1
In the case of a string, it will return the smallest considering the alphabetical order.
And understand that space comes before the a. That way, space needs to be removed before the sorting of the function returns the result you expect. I changed your array a little to understand the problem. In example 2, you expect the result to be "a", but the return is " arrozz ".
print('Exemplo 2')
menor = min(["joão", " b ", " arrozz ", "a"])
print(menor.strip())
Out[]: Exemplo 2
Out[]: arrozz
To get the expected result, use the key parameter passing a lambda function that will strip for each element.
print('Exemplo 3')
menor = min(["joão", " b ", " arrozz ", "a"], key=lambda x: x.strip())
print(menor.strip())
Out[]: Exemplo 3
Out[]: a
If what you need is the string with the smallest number of characters ignoring the spaces, use the lambda function below. It will return the size of each string (without spaces) to be evaluated by the min function.
print('Exemplo 4')
menor = min(["joão", " b ", " arrozz ", "a"], key=lambda x: len(x.strip()))
print(menor.strip())
Out[]: Exemplo 4
Out[]: b
Output must return only the character
a
or the actual value in the list including whitespace?– Woss
the output must return only {a}
– d. fritoti
You can [Edit] the question and add the code you tried to do?
– Woss
without spaces, and without parentheses
– d. fritoti
of course, I edit now
– d. fritoti
finished work friend, edited the question! See if you are better
– d. fritoti
Only the
()
in the method callstrip
. Put them on and it should work.– Woss
OK, I’ve done it. Thank you very much
– d. fritoti
Let’s go continue this discussion in chat.
– d. fritoti