Python program to find N largest and smallest elements from the list

N largest and smallest elements from the Python list: In this tutorial, we are going to learn how to find N largest and smallest elements from the given Python list. By Yash Khandelwal Last updated : June 25, 2023

Problem statement

Given a Python list, we have to write a Python program to find N largest and smallest elements from the given Python list.

Example

Consider the below example with sample input/output:

Input: [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
N = 2
Output:
Largest: [42, 37]
Smallest: [-4, 1]

Finding N largest and smallest elements from the Python list

The following are the different ways to find the N largest and smallest elements from the given Python list:

  1. Simple way: Using list.append() and remove() methods
  2. Using heapq.nlargest() and heapq.nsmallest() methods

1. Using list.append() and remove() methods

By using the conditional statement, you can find the N largest and smallest elements.

Logic

  1. Define the function name largest_ele and smallest_ele.
  2. Pass two arguments in a function (l,n) : l is the list and n is the number of elements.
  3. Run the for loop n times
  4. In the loop find the maximum of the given list and append it to another list using the list.append() method.
  5. And after appending to another list remove the maximum element from the list using the remove() method.

Example

# N largest and smallest element in a list

# function to find n largest element
def largest_ele(l, n):
    nLargestList = []
    for i in range(n):
        nLargestList.append(max(l))  # append max of list in a new list
        l.remove(max(l))  # remove max of list from the list
    return nLargestList


# function to find n largest element
def smallest_ele(l, n):
    nSmallestList = []
    for i in range(n):
        nSmallestList.append(min(l))  # append min of list in a new list
        l.remove(min(l))  # remove min of list from the list
    return nSmallestList


# Main code
list1 = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
n = 2

# Print lists
print("Original list:", list1)

result = largest_ele(list1, n)
print("N largest elements:", result)

result = smallest_ele(list1, n)
print("N smallest elements:", result)

Output

Original list: [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
N largest elements: [42, 37]
N smallest elements: [-4, 1]

2. Using heapq.nlargest() and heapq.nsmallest() methods

If you are looking for the N smallest or largest items and N is small compared to the overall size of the collection, these functions provide superior performance.

Logic

  1. Import the heapq module
  2. Give the list
  3. Now use the function heapq.nlargest(n,l) and heapq.nsmallest(n,l) from the module to find the largest and the smallest numbers.

Example

# using the inbuilt module methods
# heapq.nlargest() and heapq.nsmallest()

# Import module
import heapq

# list
list1 = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
n = 2

print("BY heapq.nlargest: ", heapq.nlargest(n, list1))
print("BY heapq.nsmallest: ", heapq.nsmallest(n, list1))

Output

BY heapq.nlargest:  [42, 37]
BY heapq.nsmallest:  [-4, 1]

Note: The nlargest() and nsmallest() methods are most appropriate if you are trying to find a relatively small number of items. If you are simply trying to find the single smallest or largest item (N=1), it is faster to use min() and max().

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.