Considering the title of your question
Function that returns the element with more characters from a list
The built-in function max python does what you need.
l = ['A', 'ABCDEFG', 'ABC']
max(l, key=len)
'ABCDEFG'
Considering the remark you made in the question
Obs: parameters must be strings or nested list containing strings!
The function below with small adjustments for cases where list items are not strings, I believe it is useful.
def max_in_list(l):
max_value = ''
for _ in l:
if isinstance(_, list):
max_value = max_in_list(_)
if len(_) > len(max_value):
max_value = _
return(max_value)
Execution must return the item with the highest character number, whether it is inside a nested list or outside.
ll = [ ['asas', 'sasas', 'a'], [ ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a' ] ] ]
max_in_list(ll)
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
ll = [ ['asas', 'sasas', 'a'], ['ssasas', 's', 'aaaaaaaaaaaaaaa'], 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
max_in_list(ll)
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
max(['A', 'ABCDEFG', 'ABC', 'B'])returns'B', then the first part of your answer is wrong. The functionmaxreturns the largest string in alphabetical order, without taking into account their size.– Woss
Adinanp, thanks for the explanation, it turns out the idea is not to use native functions in python. The teacher does not allow, my mistake not punctuate it in the statement,!
– GratefullyDead
Anderson, you’re right. I saw that in your answer you make the right use of
maxwith the argumentkey. I edited my reply, thank you for pointing out the error.– adinanp