Dark Mode On/Off

# Python program to find N largest elements from a list

In this tutorial, we will write a Python program to find N largest elements from a list. List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. We will be following various approaches to find the N largest elements in a list.

For a given list of numbers, the task is to find the N largest numbers in the list. Where N is less than or equal to the size of the list.

Input: [4, 1, 7, 3, 14, 9, 6] n=2

Output: [14, 9]

Input: [8, 10, 3, 11, 19, 17] n=3

Output: [19, 17, 11]

## Approach to find N largest elements from a list

For executing this program in Python, there are multiple approaches that we can follow-

1. By traversing the list N times and find the maximum and remove it from the list
2. By sorting the list and printing the N maximum element

We will discuss both the approaches separately

## Approach 1: Traversing list

In this approach, we will traverse the list for finding the maximum element in the list. Once found we will store it in another list and remove the maximum element from the original list. Repeat this process N times, to get N largest elements in the list.

For removing an element from a list, we will use the remove() function. To add an element in the list we will use the append() function.

### Algorithm

Follow the algorithm to understand the approach better

Step 1- Define a function to calculate n max elements

Step 2- Declare a list that will store the N max elements

Step 3- Run a loop for finding N max elements

Step 4- Traverse the entire list and find the maximum element

Step 5- After getting the maximum elements add it to the result list

Step 6- Remove the max element from the main list

Step 7- This process will be repeated N times and we will get N maximum elements

Step 8- Return the resulting list

Step 9- Declare list values and N

Step 10- Call the function and print the result

### Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

``````#N largest elements

#function
def N_max_elements(list, N):
result_list = []

for i in range(0, N):
maximum = 0

for j in range(len(list)):
if list[j] > maximum:
maximum = list[j]

list.remove(maximum)
result_list.append(maximum)

return result_list

#test
list1 = [2, 6, 41, 85, 0, 3, 7, 6, 10]
N = 2

print(N, "max elements in ",list1)

# Calling and printing the function
print(N_max_elements(list1, N))``````

2 max elements in [2, 6, 41, 85, 0, 3, 7, 6, 10]
[85, 41]

## Approach 2: Sorting list and printing N max elements

In this approach, we will first sort the elements of the list. For sorting the list we will use the predefined sort() function in Python. It will sort the elements in the list in ascending order. Then, we will print the elements present at the N positions from last after sorting. This will give us the N largest elements in the list.

### Algorithm

Follow the algorithm to understand the approach better

Step 1- Declare a function that will find the N largest numbers

Step 2- Use sort() method to sort the list

Step 3- Return the N max elements in the list

Step 4- Take input of list

Step 5- Take input of N

Step 6- Call the function and print the value returned by it

### Python Program

Look at the program to understand the implementation of the above-mentioned approach.

``````#N largest elements
#sorting
#function
def N_max_elements(list, N):
list.sort()
return list[-N: ]

#input
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
n=int(input("Enter N"))
print(n, "max elements in ",li)

# Calling and printing the function
print(N_max_elements(li, n))``````

Enter size of list 5
Enter element of list 4
Enter element of list 3
Enter element of list 2
Enter element of list 8
Enter element of list 3
Enter N2
2 max elements in [4, 3, 2, 8, 3]
[4, 8]

## Conclusion

In this tutorial, we have discussed two approaches for finding and printing N maximum elements in a list. We have used the concept of traversing for N maximum elements. For the second approach, we have used sorting to arrange the list in ascending order. We have also used built-in functions like remove(), append() and sort() in our tutorial.