- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to find N largest elements from a list
In this example, we will see how to find the N largest elements from a List. The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type
Let’s say the following is the input list −
[25, 18, 29, 87, 45, 67, 98, 5, 59]
The following is the output displaying the N largest element from the list. Here, N = 3 −
[98, 87, 67]
Python program to find N largest elements from a list with for loop
We will use a for loop here to find the N largest elements from a List −
Example
def LargestFunc(list1, N): new_list = [] for i in range(0, N): max1 = 0 for j in range(len(list1)): if list1[j] > max1: max1 = list1[j]; list1.remove(max1); new_list.append(max1) print("Largest numbers = ",new_list) # Driver code my_list = [12, 61, 41, 85, 40, 13, 77, 65, 100] N = 4 # Calling the function LargestFunc(my_list, N)
Output
Largest numbers = [100, 85, 77, 65]
Python program to find N largest elements from a list using the sort()
We will use a built-in function sort() to find the N largest elements from a List −
Example
# Create a List myList = [120, 50, 89, 170, 45, 250, 450, 340] print("List = ",myList) # The value of N n = 4 # First, sort the List myList.sort() # Now, get the largest N integers from the list print("Largest integers from the List = ",myList[-n:])
Output
List = [120, 50, 89, 170, 45, 250, 450, 340] Largest integers from the List = [170, 250, 340, 450]
- Related Articles
- Program to create a list with n elements from 1 to n in Python
- Program to find largest sum of non-adjacent elements of a list in Python
- Python program to remove duplicate elements from a Doubly Linked List\n
- Python program to find largest number in a list
- Program to find the largest grouping of anagrams from a word list in Python
- Python program to find the largest number in a list
- Program to find duplicate item from a list of elements in Python
- Program to find largest distance pair from two list of numbers in Python
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Program to find lexicographically largest mountain list in Python
- Python program to find the second largest number in a list
- Python program to remove Duplicates elements from a List?
- Python Program to Remove Palindromic Elements from a List
- Program to find only even indexed elements from list in Python
- Program to find sum of odd elements from list in Python

Advertisements