: , . , :) . , :
largest = [max(abs(x) for x in l[i:i+n]) for i in xrange(0, len(l), n)]
largest = [max(abs(x) for x in l[i:i+n]) for i in range(0, len(l), n)]
Python3.
: ( ) , , numpy . , Python. (- ).
, :
In [1]: l = [1, 2, 4, 5, 4, 5, 6, 7, 2, 6, -9, 6, 4, 2, 7, 8]
In [2]: n = 4
A. , set. , sort N () :
In [3]: sorted(list(set(l)))[-n:]
Out[3]: [5, 6, 7, 8]
B. heapq:
In [7]: import heapq
In [8]: heapq.nlargest(n, set(l))
Out[8]: [8, 7, 6, 5]
Of course, you can “wrap” a technique A or B into some kind of function convenient for a person, for example def get_largest(seq, n): return sorted(list(set(l)))[-n:]. Yes, I commented on some details, for example, the appeal IndexError. You must keep this in mind when writing code.
C. If your list is very long and you need to perform many of these operations as fast as Python, you should use special third-party libraries such as numpyor bottleneck.