
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Element with Maximum Value in Python List
List is one of the most commonly used data structures provided by Python. List is a data structure in Python that is mutable and has an ordered sequence of elements. Here is a simple example of a list of integer values -
lis= [1,2,3,4,5] print(lis)
If you execute the above program, it produces the following output
[1, 2, 3, 4, 5]
In this article, we will look at different ways to find the maximum of the list in Python. For the above list the maximum or largest element of the list is 5.
Using the max() function
In this approach, we use the max() function to find the maximum valued element. The max() function returns an element from the list with the maximum value.
Example
The following is an example code for finding the maximum element of a list using the max() function.
list1 = [1, 0, 4, 5, 100] print("Largest element of the list is:", max(list1))
Output
When you run the program, it will show this output:
Largest element of the list is: 100
Using the sort() method
Here we will find the maximum element of a list using the sort() method. The sort() method which is provided by python is used to sort the elements of the list in ascending or descending order. Default, it will sort in ascending order. After sorting the largest element is at the last position of the list, so we print that element.
Example 1
The following example code finds the largest element of the list using sort() method.
list1 = [100, 1, 5, 80, 20] list1.sort() print("Largest element of the list is:", list1[ 1])
Output
After running the program, you will get this result:
Largest element of the list is: 5
Example 2
Following is another example to find the element from a Python list with a maximum value using the sort() method.
the_list = [54, 57, 827, 74, 91, 74, 62] the_list.sort() maximum_element = the_list [-1] print("The Maximum element of the given list is: ", maximum_element)
Output
This output will be displayed when the program runs-
The Maximum element of the given list is: 827
Without using the Built-in Function
In this method, we will not use built-in functions, rather, we use a loop to find the largest element in a list.
Here, initially assume the first element as the maximum, and then we iterate over a for loop where we compare it with other elements in the list. While comparing with elements in the list, we change the max variable if the element is greater than the compared maximum element. Finally, after getting terminated from the loop, we get the maximum element.
Example
The following is an example to get the maximum element from a list.
def MaxElement(lst): max = lst[0] for ele in lst: if ele > max : max = ele return max lst = [100, 1, 5, 80, 20] print("Largest element of the list is:", MaxElement(lst))
Output
You will see this result after executing the program:
Largest element of the list is: 100