Informatics Practices
Write a program to find the largest and the second largest elements in a given list of elements.
Python List Manipulation
10 Likes
Answer
nums = eval(input("Enter the list: "))
first = nums[0]
second = nums[0]
for num in nums[2:]:
if num > first:
second = first
first = num
elif num > second and num != first:
second = num
print("Largest element:", first)
print("Second largest element:", second)
Output
Enter the list: [2, 8, 10, 18, 15, 6]
Largest element: 18
Second largest element: 15
Answered By
3 Likes
Related Questions
Differentiate between append() and insert() methods of list.
Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.
Write a program to read a list of n integers and find their median.
Note: The median value of a list of values is the middle one when they are arranged in order. If there are two middle values, then take their average.
Hint: Use an inbuilt function to sort the list.
Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements, i.e., all elements occurring multiple times in the list should appear only once.