Write a python program to print Nlargest numbers

Write a python program to print Nlargest numbers

Using startswith()
def myfun():
    list1 = [12, 34, 67, 98, 90, 68, 55, 54, 64, 35]
    list2= sorted(list1, reverse=True)
    print(list2[:5])

if __name__=="__main__":
    myfun()


Output:

[98, 90, 68, 67, 64]


Using heapq module
import heapq
def myfun():
    list1 = [12, 34, 67, 98, 90, 68, 55, 54, 64, 35]
    list2= heapq.nlargest(5, list1)
    print(list2)

if __name__=="__main__":
    myfun()



Output:

[98, 90, 68, 67, 64]



More Questions


40 . Write a python program to print Nlargest numbers
41 . Write a python program to print Nsmallest numbers
42 . Write a python program to check two strings are Anagram or not
43 . Write a python program to checking internet speed
44 . Write a python program to print reserved keywords
45 . Write a python program to print properties and methods
46 . Write a python program to open a website using python
47 . Write a python program to print most frequent letter in a string
48 . Write a python program to check memory sizes of list ,set ,tuple
49 . How do you confirm if an object is iterable using code?
50 . Write a python program to sort list with sorted & lambda



For latest job updates join Telegram Channel: https://t.me/sateeshm