Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the maximums max function

Tags:

python

max

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?

like image 203
gabber12 Avatar asked May 30 '12 19:05

gabber12


People also ask

How do you find the maximum value in Excel with multiple conditions?

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.

How does Max () work in Python?

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.


1 Answers

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]
like image 77
Hugh Bothwell Avatar answered Sep 20 '22 22:09

Hugh Bothwell