Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

# find the n smallest and greatest numbers in list

# find the n smallest and greatest numbers in list
import heapq
numbers = [10, 40, 25, 500, 90, 59, 320, 200, 100, 800]
print(heapq.nlargest(4, numbers))
# [800, 500, 320, 200]
import heapq
numbers = [10, 40, 25, 500, 90, 59, 320, 200, 100, 800]
print(heapq.nsmallest(4, numbers))
# [10, 25, 40, 59]
 
PREVIOUS NEXT
Tagged: #find #smallest #greatest #numbers #list
ADD COMMENT
Topic
Name
5+9 =