Here's a simple function which does this in one pass:
def maxes(a, key=None):
if key is None:
key = lambda x: x
m, max_list = key(a[0]), []
for s in a:
k = key(s)
if k > m:
m, max_list = k, [s]
elif k == m:
max_list.append(s)
return m, max_list
In action:
In [11]: maxes(['a', 'ab', 'a', 'cd'], key=len)
Out[11]: (2, ['ab', 'cd'])
This may, or may not be faster than running the list comprehension mentioned by the other poster and certainly faster than the sorting... but a little testing suggests its faster:
For a example of strings:
In [20]: a = [''.join(random.choice('abc') for _ in xrange(random.randint(1, 100)))
for i in xrange(1000)]
In [21]: %timeit maxes(a, key=len)
10000 loops, best of 3: 53 µs per loop
In [22]: %timeit m = max(map(len, a)); [s for s in a if len(s) < m]
10000 loops, best of 3: 104 µs per loop
In [23]: %timeit sorted_a = sorted(a, key=len, reverse=True); [s for s in a if len(s) == len(sorted_a[0])]
1000 loops, best of 3: 322 µs per loop
If we look at integers, with a key:
In [30]: a = [random.randint(1, 10000) for i in xrange(1000)]
In [31]: %timeit maxes(a, key= lambda x: x**2)
10000 loops, best of 3: 150 µs per loop
In [32]: %timeit m = max(a, key=lambda x: x**2); [s for s in a if s**2 < m]
1000 loops, best of 3: 183 µs per loop
In [33]: %timeit sorted_a = sorted(a, key=lambda x: x**2, reverse=True); [s for s in a if s ** 2 == sorted_a[0] ** 2]
1000 loops, best of 3: 441 µs per loop
However, without a key the list comprehension is better:
In [34]: %timeit maxes(a)
10000 loops, best of 3: 98.1 µs per loop
In [35]: %timeit m = max(a); [s for s in a if s < m]
10000 loops, best of 3: 49.2 µs per loop
In [36]: %timeit sorted_a = sorted(a, reverse=True); [s for s in a if s == sorted_a[0]]
10000 loops, best of 3: 152 µs per loop
This is expected since the redundant key code is still being applied, if we were toremove that logic (replace calls to key(x) with just x) the function is again slightly faster:
In [37]: %timeit maxes2(a)
10000 loops, best of 3: 39.7 µs per loop