View on GitHub

python-for-kids

Learning and exercise content on Python for Kids

Day 28 - Find Smallest and Largest number(s) from a collection

If you have a collection and you need to find the top one or few largest numbers or bottom one or few smallest numbers then you can use heapq

import heapq 

#Find top Large/bottom small numbers
nums = [1,3,4,6,8,9,22,44]

#Find top 3 largest numbers
topThree = heapq.nlargest(3, nums)
print("Three Largest..")
print(topThree)

print("")

#Find bottom 2 smallest numbers
smallTwo = heapq.nsmallest(2, nums)
print("Two Smallest..")
print(smallTwo)

If the list is complex, say each element contains multiple values as below where we have list of students and their weights. To find the heaviest students,

import heapq

students = [
    {'name':'Student1', 'weight':33.5},
    {'name':'Student2', 'weight':36},
    {'name':'Student3', 'weight':35},
    {'name':'Student4', 'weight':53.5},
    {'name':'Student5', 'weight':43.5}
]

heavyStudent = heapq.nlargest(1, students, key= lambda s : s['weight'])
print(heavyStudent)


Back to Index