Python | Find Largest Number in List

Python’s List Object type is quite useful for storing data while dealing with Scientific Computations where large amount of data need to be processed. Anyway, in this article => I’ll discuss How to find Maximum Number from a List of Numbers in Python?

For finding maximum number out of all numbers in a Python List. Any of following three approaches can be used =>

  • Sort Numbers in Ascending Order, then last number in List will be Maximum
  • Using Python’s Built-in List Function – max(), pass list to this function and it just returns Maximum Number in List
  • Loop through each element in List using For Loop and compare individual numbers with last seen Maximum Number => This approach is Worst performing as its making too much comparisons

Let’s discuss each one of these algorithmic approaches for Finding Maximum Number in a Python List.

Approach 1 => Find Maximum Number in a Python List

  1. Define Python List
  2. Call sort() function on list using list_name.sort() statement
  3. Access last number in list using list_name[-1]
  4. Last Number in list will be Largest Number
# Python Program to Find largest number in List

a_list = [19, 82, 19, 17, -1, 2, -37]        # Define List

a_list.sort()                                # Sort Nunbers in List in ascending order

print("Largest Number in List is => ", a_list[-1])

Output of Above Code

Largest Number in List is =>  82

Approach 2 => Find Largest Number in a Python List

  1. Define Python List
  2. Pass list as parameter to max() function using max(list_name) statement
  3. Number returned by max() function would be largest in List
# Python Program to Find largest number in List

a_list = [19, 82, 19, 17, -1, 2, -37]        # Define List

maximum_number = max(a_list)                # Call max() function on List

print("Largest Number in List is => ", maximum_number)

Output of Above Code

Largest Number in List is =>  918

Approach 3 => Finding Maximum Number out of a Python List

  1. Define Python List
  2. Assume first number in Python List to be Largest
  3. Loop over all numbers in List using for loop and compare each number with the assumed maximum number, update assumed maximum number to current iteration number if its greater than assumed maximum number
  4. At the end of For loop return assumed maximum number
# Python program to find largest number in a list

a_list = [19, 38, 21, 34, 89]                 # Define Python List

def maximumNum(a_list):
	# Assuming first number in list to be largest initially
	# Assign it to variable "max"
	max = a_list[0]

	# Now traverse through the list using For Loop and compare
	# each number with "max" value. Whichever number is
	# largest assign that value to "max".
	for x in a_list:
		if x > max :
			max = x
	
	# after complete traversing the list
	# return the "max" value
	return max

print("Largest Number in List is => ", maximumNum(a_list))

Output of Above Code

Largest Number in List is =>  672

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts