Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Python for Beginners: Largest Element in a List in Python

$
0
0

We often use lists to store numbers. In this article, we will discuss different ways to find the largest element in a list in python.

Largest Element in a List Using the sort() Method in Python

If a list is sorted, the largest element resides at the end of the list and we can access it using the syntax list_name[-1]. If the list is sorted in descending order, the largest element in the list lies at the first position i.e. index 0. We can access it using the syntax list_name[0]

When a list is unsorted, finding the largest number is a slightly different task. In this situation, we can first sort the list and then access the last element of the sorted list. Alternatively, we can check each element of the list and then find the largest element in the list.

To find the largest element by sorting the list, we can use the sort() method. The sort() method, when invoked on a list, sorts the list in ascending order. After sorting, we can get the largest element of the list from the index -1 as follows.

myList = [1, 23, 12, 45, 67, 344, 26]
print("The given list is:")
print(myList)
myList.sort()
print("The maximum element in the list is:", myList[-1])

Output:

The given list is:
[1, 23, 12, 45, 67, 344, 26]
The maximum element in the list is: 344

Using the sorted() Method to Find the Largest Element in A List

If you are not allowed to sort the original list, you can use the sorted() function to sort the list. The sorted() function takes a list as an input argument and returns a sorted list. After obtaining the sorted list, we can find the largest element of the list at index -1 as follows.

myList = [1, 23, 12, 45, 67, 344, 26]
print("The given list is:")
print(myList)
newList = sorted(myList)
print("The maximum element in the list is:", newList[-1])

Output:

The given list is:
[1, 23, 12, 45, 67, 344, 26]
The maximum element in the list is: 344

Largest Element in a List Using a Temporary Variable  in Python

Sorting a list requires O(n*log(n)) time, where n is the number of elements in the list. For larger lists, it may take a long time to sort the list before we can obtain the largest element. Using an alternative approach, we can find the largest element in the list in O(n) time. 

In this approach, we will create a variable myVar and initialize it with the first element of the list. Now, we will consider that myVar has the largest element. After that, we will compare myVar with each element of the list. If any element is found to be greater than myVar, we will update the value in myVar with the current value. After traversing the entire list, we will get the largest element of the list in the myVar variable. You can observe this in the following example.

myList = [1, 23, 12, 45, 67, 344, 26]
print("The given list is:")
print(myList)
myVar = myList[0]
for element in myList:
    if element > myVar:
        myVar = element
print("The maximum element in the list is:", myVar)

Output:

The given list is:
[1, 23, 12, 45, 67, 344, 26]
The maximum element in the list is: 344

Largest Element in a List Using the max() Function in Python

Instead of the above approach, you can directly use the max() function to find the largest element of the list. The max() function takes the list as an input argument and returns the largest element of the list as shown below.

myList = [1, 23, 12, 45, 67, 344, 26]
print("The given list is:")
print(myList)
myVar = max(myList)
print("The maximum element in the list is:", myVar)

Output:

The given list is:
[1, 23, 12, 45, 67, 344, 26]
The maximum element in the list is: 344

Conclusion

In this article, we have discussed various to find the largest element in a list in python. To learn more about lists in python, you can read this article on list comprehension in python. You might also like this article on set comprehension in python.

The post Largest Element in a List in Python appeared first on PythonForBeginners.com.


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>