Python program to find second largest number in a listGiven a list of numbers, the task is to write a Python program to find the second largest number in the given list. Show
Method 1: Sorting is an easier but less optimal method. Given below is an O(n) algorithm to do the same. Python3
Output
Second highest number is : 45
Method 2: Sort the list in ascending order and print the second last element in the list. Python3
Output Second largest element is: 45 Method 3: By removing the max element from the list Python3
Output
45
Method 4: Find max list element on inputs provided by the user Python3
Output: Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 1 Enter elements: 99 Second Largest element is: 19Method 5: Traverse once to find the largest and then once again to find the second largest. Python3
Output: 45
Article Tags :
Python Python Programs
Python list-programs python-list Practice Tags :
python-list Find the maximum elements in the first and the second halves of the ArrayGiven an array arr[] of N integers. The task is to find the largest elements in the first half and the second half of the array. Note that if the size of the array is odd then the middle element will be included in both halves.
Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: Calculate the middle index of the array as mid = N / 2. Now the first halve elements will be present in the subarray arr[0…mid-1] and arr[mid…N-1] if N is even. C++
Java
Python3
C#
Javascript
Output:
12, 14
Time Complexity: O(n) Auxiliary Space: O(1)
Article Tags :
Arrays Mathematical
Constructive Algorithms Practice Tags :
Arrays Mathematical Python | Create two lists with first half and second half elements of a listHere, we are going to learn how to create two lists with first half and second half elements of a given list in Python? Given a list, and we have to create two lists from first half elements and second half elements of a list in Python. Example: Input: list: [10, 20, 30, 40, 50, 60] Output: list1: [10, 20, 30] list2: [40, 50, 60]Logic:
Program: # define a list list = [10, 20, 30, 40, 50, 60] # Create list1 with half elements (first 3 elements) list1 = list [:3] # Create list2 with next half elements (next 3 elements) list2 = list [3:] # print list (s) print "list : ",list print "list1: ",list1 print "list2: ",list2Output list : [10, 20, 30, 40, 50, 60] list1: [10, 20, 30] list2: [40, 50, 60]Using list[0:3] and list[3:6] instaed of list[:3] and list[3:]We can also use list[0:3] instead of list[:3] to get first 3 elements and list[3:6] instead of list[3:] to get next 3 elements after first 3 elements. Consider the program: # define a list list = [10, 20, 30, 40, 50, 60] # Create list1 with half elements (first 3 elements) list1 = list [0:3] # Create list2 with next half elements (next 3 elements) list2 = list [3:6] # print list (s) print "list : ",list print "list1: ",list1 print "list2: ",list2Output list : [10, 20, 30, 40, 50, 60] list1: [10, 20, 30] list2: [40, 50, 60]ADVERTISEMENT By considering length of the listLet suppose list has n elements, then we can use list[0:n/2] and list[n/2:n]. Consider the program: If there are ODD numbers of elements in the list, program will display message "List has ODD number of elements." And exit. # define a list list = [10, 20, 30, 40, 50, 60] # get the length of the list n = len(list) # condition to check length is EVEN or not # if lenght is ODD, show message and exit if( n%2 != 0 ): print "List has ODD number of elements." exit() # Create list1 with half elements (first 3 elements) list1 = list [0:n/2] # Create list2 with next half elements (next 3 elements) list2 = list [n/2:n] # print list (s) print "list : ",list print "list1: ",list1 print "list2: ",list2Output list : [10, 20, 30, 40, 50, 60] list1: [10, 20, 30] list2: [40, 50, 60]Python List Programs » Python | Create three lists of numbers, their squares and cubes Python | Iterate a list in reverse order ADVERTISEMENT Preparation Aptitude Questions MCQs Find Output Programs What's New
ADVERTISEMENT Top Interview Coding Problems/Challenges!
Comments and Discussions! Please enable JavaScript to view the comments powered by Disqus. ADVERTISEMENT Python program to find second largest number in a listIn this tutorial, we will write a Python program to find the second largest number in 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 second largest number in a list. For a given list of numbers, the task is to find the largest number in the list. Input: [11, 5, 2, 8, 4, 19] Output: 11 Input: [2, 11, 18, 23, 6] Output: 18 Find Maximum Value in a List in PythonPython Python List Created: November-16, 2020 | Updated: December-10, 2020 This tutorial will demonstrate how to find the maximum value in a list in Python. A few scenarios and data types will be covered, from a simple integer list to a more complex structure such as an array within an array. Python program to find the second largest number in a listPythonServer Side ProgrammingProgramming In this article, we will learn about the solution to the problem statement given below. Problem statement− We are given a list, we need to display the second largest number in a list. There are three approaches to solve the problem− Python Program to Create Two Lists with First Half and Second Half Elements of Given List
1: Python program to create two lists with first half and second half elements of a given listuse the following steps to write a python program to create list in first half of the elements with the second half of the elements:
# write a python program to create two lists with first half and second half
#elements of a given list.
NumList = []
Number = int(input("How many elements in list :- "))
# condition to check given number is even or odd
if( Number%2 != 0 ):
print("This program will not accept odd number.")
exit()
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element :- " %i))
NumList.append(value)
#number half
num = int(Number/2)
# Create list1 with half elements (first 3 elements)
list1 = NumList[:num]
# Create list2 with next half elements (next 3 elements)
list2 = NumList[num:]
# print list (s)
print("list : ",NumList)
print("list1: ",list1)
print("list2: ",list2)
After executing the program, the output will be: How many elements in list :- 6 Please enter the Value of 1 Element :- 1 Please enter the Value of 2 Element :- 2 Please enter the Value of 3 Element :- 3 Please enter the Value of 4 Element :- 4 Please enter the Value of 5 Element :- 5 Please enter the Value of 6 Element :- 6 list : [1, 2, 3, 4, 5, 6] list1: [1, 2, 3] list2: [4, 5, 6]2: Python program to create two lists with first half and second half elements of a given list using range slicinguse the following steps to write a python program to create list in first half of the elements with the second half of the elements using range slicing: # write a python program to create two lists with first half and second half
#elements of a given list.
NumList = []
Number = int(input("How many elements in list :- "))
# condition to check given number is even or odd
if( Number%2 != 0 ):
print("This program will not accept odd number.")
exit()
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element :- " %i))
NumList.append(value)
# divide by 2 the length of list
n = int(Number/2)
# Create list1 with half elements (first 3 elements)
list1 = NumList [0:n]
# Create list2 with next half elements (next 3 elements)
list2 = NumList [n:Number]
# print list (s)
print("list : ",NumList)
print("list1: ",list1)
print("list2: ",list2)
After executing the program, the output will be: How many elements in list :- 6 Please enter the Value of 1 Element :- 9 Please enter the Value of 2 Element :- 8 Please enter the Value of 3 Element :- 7 Please enter the Value of 4 Element :- 6 Please enter the Value of 5 Element :- 5 Please enter the Value of 6 Element :- 4 list : [9, 8, 7, 6, 5, 4] list1: [9, 8, 7] list2: [6, 5, 4] |