Home Python How to find the second largest value of a Python List

# How to find the second largest value of a Python List

Python list is one of the most useful data structures of Python. It is very similar to the arrays in Javascript and C++. However, like the multidimensional arrays, we can also create multidimensional lists known as nested lists. In this tutorial, we discussed four amazing methods for finding the second largest number of a python list.

To follow this tutorial, you must have python installed in your system. It is also recommended to update python to its latest version so many of the latest features of python can be used by us in our programs. Installing an IDE is also a good choice as it let us write code faster by providing many important features. There are many available IDEs like the open-source visual studio code, a cross-platform tool and provides many excellent features for coding.

## Finding the second largest value of a Python List

Now, let us begin our tutorial by taking a look at each of the methods one by one.

### Method 1

This is a simple method to find the second largest element of a list. In this method, we will first sort the python list using the sort() method or sorted() function. After sorting the list, we can find the second largest element by selecting the second element. But while using this method, we also need to check if the first and the second element of the list are equal, and if they are equal, then we need to again check for the third element and so on until we get an unequal element. If we find the unequal element, then we will display the number as the second-largest number on the list. See the following code for a practical illustration.

```# The python list in which we need to find the second largest element
list1 = [1, 3, 5, 1,333, 44, 1000, 23, 1000, 333]
# displaying the list
print(f" [+] Given list : {list1}")

# Sorting the given list
list1.sort()
# displaying the sorted list
print(" [+] Sorted List : ",list1)
# accessing the right most element of the list
m = list1[-1]

def check_max(m):
if max(list1) == m:
return True
else:
return False

while check_max(m):
list1.remove(m)

print(f" [+] The second largest number of the list is {max(list1)}")```

You can copy the above code using the copy button at the top of the block and run it using python3. First, let us look at what is happening in the above code.

In the first line of code, we have created a python list containing some integers, and the list has been displayed in the console using the print() function of python. In the third line, we used the sort() method of the python list without any argument, so it will sort the list in ascending order. Next, the sorted list has been displayed in the console using the print() function. Finally, we use the negative indexing(-1) method of the python list to get the max element(the last element) of the sorted list.

After that, we use a while loop of python with a function that will check the next element of the sorted list, and if the element is the max element, it will be removed from the list using the remove() method of python list. After removing the max element, the new max element of the python list will be the second max element of the list. Thus, we can display the max element of the list using the max() function as shown in the above code. On running the above code, we will get the output as shown in the below image.

### Method 2

In this method, we will first sort the list in ascending order using the sort() method or sorted() function. We also get the max element of the list here and remove the max element using the remove() method by passing the max element as an argument to the remove() method. The last element of the resultant list is the required second largest element of the list. The following code shows a simple demonstration of the method.

```# The python list in which we need to find the second largest element
list1 = [1, 3, 5, 1,333, 44, 1000, 23, 1000, 333]
# displaying the list
print(f" [+] Given list {list1}\n")
# getting the maximum of the list
list1.sort()
m = list1[-1]

def check_max(m):
if max(list1) == m:
return True
else:
return False

while check_max(m):
list1.remove(m)

print(f" [+] The second largest element of the list is {list1[-1]}")```

The above code is almost the same as the previous code, but the only difference here is that we use the negative indexing in both places to get the max element of the list instead of using the max() function. So there will be some differences in the time taken for the execution of both the programs.

### Method 3

This method is almost similar to the previous method. In this method, first, we will find the max element and remove it from the list using the remove() method as done in the previous method. Then we again use the max() method to find the max element of the list. The main difference of this method from the previous two methods is that it does not require sorting. The following code shows a simple demo of the method.

```# The python list in which we need to find the second largest element
list1 = [1, 3, 5, 1,333, 44, 1000, 23, 1000, 333]
# displaying the list
print(f" [+] Given list {list1}\n")

# getting the maximum of the list
m = max(list1)

def check_max(m):
if max(list1) == m:
return True
else:
return False

while check_max(m):
list1.remove(m)

print(f" [+] The second largest number of the list is {max(list1)}")```

In this method, we have not sorted the python list and directly used the max() function of the python. So in the first line of code, we have created a python list containing some integers. Then we use the max() function of the python and pass the python list as its argument so it will calculate the max integer of the list.

After that, we use a while loop with a function check_max() that will check all the occurrences of the maximum integer, i.e., 1000, in the list, and remove all of them using the remove() method of python. We again use the max() function and pass the list as its argument to return the max element of the new list, which will be the second-largest element of the list. On running the above code, we will get the output as shown in the below image.

### Method 4

This method is somewhat different from the methods that we have discussed till now. In this method, we will first remove all the duplicate elements of the list by converting the list into a set and again convert it into a list. Then the second last element of the list will be the second-largest element, and it can be obtained by using negative indexing. The following code shows an illustration of the method.

```# The python list in which we need to find the second largest element
list1 = [1, 3, 5, 1,333, 44, 1000, 23, 1000, 333]
# displaying the list
print(f" [+] Given list {list1}\n")

# transforming the list into a set to get the unique values
set1 = set(list1)
# Again transforming the set into a list
list2 = list(set1)

print(f" [+] The second largest number of the list is {list2[-2]}")```

This method is one of the best methods for finding the second max element of the list. In the above code, we first created a python list containing some integers and displayed the list using the print function as we perform in the above three methods. After that, we use type conversion to convert the python list into a python set. The benefit of doing this is that the python set can only contain unique values in sorted order, so all the duplicate values of the list will be removed in the set, and the set will be sorted. Finally, we again convert the set into a python list to get a python list containing unique elements in sorted order. Now, the second largest element of this list can be accessed by using negative indexing of -2.

On running the above code we will get the output as shown in the below image.

### User Input

We have a look into four methods for finding the second largest number of a python list. But in all the above methods, we have used a list whose data are previously defined. Now we will see another program in which we will take the python list as an input from the user and print the second largest element of the list. See the below code for an illustration.

```# TAking the python list as input in which
# we need to find the second largest element

list1 = []
n = int(input("Enter the list size : "))
for i in range(0, n):
item = int(input())
list1.append(item)

# displaying the list
print(f"[+] Given list {list1}\n")

# transforming the list into a set to get the unique values
set1 = set(list1)
# Again transforming the set into a list
list2 = list(set1)

print(f"[+] The second largest number of the list is {list2[-2]}")```

In the above code, we first created an empty list, and we take the number of elements of the list as input from the user. After that, we used the python for loop to iterate and take each element from the console and then append that element to the empty python list we created. Finally, after getting all the user input to the list, we use method 4 that we discussed to find the second largest element of the python list. On running the above code in a python3 interpreter, we will get the output as shown in the below image.