N Largest And N Smallest In List In Python

[Solved] N Largest And N Smallest In List In Python | C - Code Explorer | yomemimo.com
Question : n-largest and n-smallest in list in python

Answered by : sundar-hk3z77f80ffk

import heapq
grades = [110, 25, 38, 49, 20, 95, 33, 87, 80, 90]
print(heapq.nlargest(3, grades))
print(heapq.nsmallest(4, grades))
[110, 95, 90]
[20, 25, 33, 38]

Source : | Last Update : Fri, 03 Sep 21

Question : find the largest and smallest numbers in a list

Answered by : courageous-cod-p3ar04wwbgln

#Exercise 3 - Find the largest and smallest number in a list
thislist = input("How many numbers do you want to enter:")
newlist = []
for x in range (0, int(thislist)): owl = int(input("Please Enter your numbers:")) newlist.append(owl)
print ("The max number entered is:", max(newlist))
print ("The min number entered is:", min(newlist))

Source : | Last Update : Thu, 10 Jun 21

Question : find the largest and smallest numbers in a list

Answered by : courageous-cod-p3ar04wwbgln

thislist = [677,8765,8765,876,470,754,6784,56789,7658,]
thislist.sort()
print ("The smallest number is: " + str(thislist[0]))
print ("The largest number is: " + str(thislist[-1]))

Source : | Last Update : Thu, 10 Jun 21

Question : python code to find the smallest number from the list

Answered by : awmrit

# list of numbers
list1 = [10, 20, 9, 69, 9]
# sorting the list
list1.sort()
# printing the first element
print("Smallest element is:", list1[0])

Source : | Last Update : Thu, 29 Sep 22

Question : Python program to find smallest number in a list

Answered by : travinth-dayalaeaswaran

# Python program to find smallest
# number in a list
 
# creating empty list
list1 = []
 
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
 
# iterating till num to append elements in list
for i in range(1, num + 1):
    ele= int(input("Enter elements: "))
    list1.append(ele)
     
# print maximum element
print("Smallest element is:", min(list1))

Source : https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/?ref=lbp | Last Update : Thu, 21 Jul 22

Question : Python | Largest, Smallest, Second Largest, Second Smallest in a List

Answered by : travinth-dayalaeaswaran

# Python prog to illustrate the following in a list
def find_len(list1):
    length = len(list1)
    list1.sort()
    print("Largest element is:", list1[length-1])
    print("Smallest element is:", list1[0])
    print("Second Largest element is:", list1[length-2])
    print("Second Smallest element is:", list1[1])
 
# Driver Code
list1=[12, 45, 2, 41, 31, 10, 8, 6, 4]
Largest = find_len(list1)

Source : https://www.geeksforgeeks.org/python-largest-smallest-second-largest-second-smallest-list/?ref=lbp | Last Update : Thu, 21 Jul 22

Question : Python program to find smallest number in a list

Answered by : travinth-dayalaeaswaran

# Python program to find smallest
# number in a list
 
# list of numbers
list1 = [10, 20, 4, 45, 99]
 
# sorting the list
list1.sort()
 
# printing the first element
print("Smallest element is:", *list1[:1])

Source : https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/?ref=lbp | Last Update : Thu, 21 Jul 22

Question : finding the largest or smallest N items in python

Answered by : mazeltov27

# Finding the largest or smallest N items using heapq
import heapq
nums = [1, 8, 2, 23, 7, -4, 19, 23, 42, 38, 2]
print(heapq.nlargest(3, nums)) # 3 largest value
print(heapq.nsmallest(3, nums)) # 3 smallest value
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'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])

Source : | Last Update : Fri, 29 Jul 22

Answers related to n largest and n smallest in list in python

Code Explorer Popular Question For C