HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions

Python – Find largest or smallest N items

By Lokesh Gupta | Filed Under: Python

Python examples to find the largest (or the smallest) N elements from a collection of elements.

Using heapq module

Python heapq module can be used to find N largest or smallest items from collections. It has two functions to help with –

  1. nlargest()
  2. nsmallest()

Find items in simple iterables

>>> import heapq

>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]

print(heapq.nlargest(3, nums))  
>>> [42, 37, 23]

print(heapq.nsmallest(3, nums)) 
>>> [-4, 1, 2]

Find items in complicated iterables

>>> portfolio = 
[
   {'name': 'IBM', 'shares': 100, 'price': 91.1},
   {'name': 'AAPL', 'shares': 50, 'price': 543.22},
   {'name': 'FB', 'shares': 200, 'price': 21.09},
   {'name': 'HPQ', 'shares': 35, 'price': 31.75},
   {'name': 'YHOO', 'shares': 45, 'price': 16.35},
   {'name': 'ACME', 'shares': 75, 'price': 115.65}
]

>>> cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
>> cheap
>>> [
		{'price': 16.35, 'name': 'YHOO', 'shares': 45}, 
		{'price': 21.09, 'name': 'FB', 'shares': 200}, 
		{'price': 31.75, 'name': 'HPQ', 'shares': 35}
	]

>>> expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
>>> expensive
>>> [
		{'price': 543.22, 'name': 'AAPL', 'shares': 50}, 
		{'price': 115.65, 'name': 'ACME', 'shares': 75}, 
		{'price': 91.1, 'name': 'IBM', 'shares': 100}
	]
If you are simply trying to find the single smallest or largest item (N=1), it is faster to use min() and max() functions.

Happy Learning !!

About Lokesh Gupta

Founded HowToDoInJava.com in late 2012. I love computers, programming and solving problems everyday. A family guy with fun loving nature. You can find me on Facebook, Twitter and Google Plus.

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Search Tutorials

  • Email
  • Facebook
  • RSS
  • Twitter

Python Tutorial

  • Python – Install In Sublime Editor
  • Python – Read/Write CSV Files
  • Python – httplib2
  • Python – Unpack Tuple
  • Python – Unpack Variable Lengths
  • Python – max() and min()
  • Python – Largest/Smallest N items
  • Python – multidict
  • Python – OrderedDict
  • Python – dictionary intersection
  • Python – Split string
  • Python – startswith
  • Python – endswith
  • Python – Priority Queue

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Java Collections
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap