data = ['str', 'frt']
max(data, key=len)
The max function returns only one of the strings.
How can I make it return both of the strings?
The length of both strings is equal, so max
should return both the strings but it returns only one so is there a way to return all max items?
The MAXIFS function in Excel can get the highest value based on one or multiple criteria. By default, Excel MAXIFS works with the AND logic, i.e. returns the maximum number that meets all of the specified conditions. For the function to work, the max range and criteria ranges must have the same size and shape.
Python max() Function The max() function returns the item with the highest value, or the item with the highest value in an iterable. If the values are strings, an alphabetically comparison is done.
You can write this as a list comprehension:
data = ['str', 'frt']
maxlen = max(map(len, data))
result = [s for s in data if len(s) == maxlen]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With