• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Largest Element in a List in Python

Largest Element in a List in Python

Author: Aditya Raj
Last Updated: June 4, 2022

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.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us