
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to find the element from a Python list with a maximum value?
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. Following is a list of integer values
Example
Following is a list of integer values.
lis= [1,2,3,4,5] print(lis)
Output
If you execute the above snippet, 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 element of the list is 5.
Using the max() function
In this method, 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
If you execute the above snippet, it produces the following 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
If you execute the above snippet, it produces the following output.
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 −
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
Following is an output of the above code −
The Maximum element of the given list is: 827
Using for loop or 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 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 code 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
If you execute the above snippet, it produces the following output.
Largest element of the list is: 100
- Related Articles
- How to find the element from a Python list with a minimum value?
- Python Program to print element with maximum vowels from a List
- Python program to find the maximum and minimum value node from a circular linked list
- Python program to find the maximum and minimum value node from a doubly linked list
- Find a pair from the given array with maximum nCr value in Python
- Get first element with maximum value in list of tuples in Python
- Find the sublist with maximum value in given nested list in Python
- Program to find maximum XOR with an element from array in Python
- How we can update a Python list element value?
- How to remove an element from a list in Python?
- Python – Display the key of list value with maximum range
- How to find an element in a List with Java?
- Write a program in Go language to find the element with the maximum value in an array
- Find a pair from the given array with maximum nCr value in C++
- Write a Python program to find the maximum value from first four rows in a given series
