How do you find the largest number in a list using for loops in Python?

How to find the largest number in a list Python using for loop example

Simple example code

numbers = [1, 2, 3, 5, 9, 6, 101, 88, 66, 6, 101, 55, -1001] maxi = numbers[0] for i in numbers: if i > maxi: maxi = i print("Greatest number: ", maxi)

Output:

How do you find the largest number in a list using for loops in Python?

Or use this code (a bit complicated)

a = [18, 52, 23, 41, 32] # variable to store largest number ln = a[0] if a else None # find largest number for i in a: if i > ln: ln = i print("Largest element is: ", ln)

Output: Largest element is: 52

Do comment if you have any doubts or suggestions on this Python list topic.

Note: IDE:PyCharm2021.3 (Community Edition)

Windows 10

Python 3.10.1

AllPython Examplesare inPython3, so Maybe its different from python 2 or upgraded versions.

How do you find the largest number in a list using for loops in Python?

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Share this:

  • Facebook
  • WhatsApp
  • LinkedIn
  • More
  • Twitter
  • Print
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • Skype
  • Email

Related

Find Largest Number in Python List

You can find largest number of a list in Python using sort() function or more basic for loop.

How do you find the largest number in a list using for loops in Python?

Using sort() function is kind of concise, but using For Loop is efficient. We will go through these two approaches with examples.

Example 1: Find the largest number using sort() function

We know that sort() builtin function sorts a list in ascending or descending order. After you sort a list, you have your largest number at the end of the list if you have sorted in ascending order or at the start of the list if you have sorted in descending order.

In the following example, we will sort the given list in ascending order. Of course, last number of the sorted list is our required largest number.

Python Program

# list a=[18, 52, 23, 41, 32] # sort the list, default is in ascending order a.sort() # largest number is the last item in the sorted list ln = a[-1] # print the largest number print("Largest element is: ",ln)Run

Output

Largest element is: 52

a[-1] fetches the last element in the list.

Example 2: Find Largest Number in List using For Loop

Though finding the largest number using sort() function is easy, using Python For Loop does it relatively faster with less number of operations. Also, the contents of the list are unchanged.

Python Program

a = [18, 52, 23, 41, 32] #variable to store largest number ln = a[0] if a else None #find largest number for i in a: if i>ln: ln=i print("Largest element is: ",ln)Run

Output

Largest element is: 52

In this example, we have assumed a list and initialized largest number ln variable to the first element of the list. If there are no elements in the list, ln is initialized with None.

Iterate the loop for each element in the list. During each iteration, we check if the largest number is less than this element. If that is the case, we update our largest number with the element.

When you complete traversing the list, you will end up with the largest number of the list in your variable.

Summary

In this tutorial of Python Examples, we learned some of the ways to find largest number in Python list with the help of well detailed Python example programs.

  • Python Program to Find Duplicate Items of a List
  • How to Insert Item at Specific Index in Python List?
  • Python – Check if Element is in List
  • Python Program to Find Smallest Number in List
  • Python List with First N Elements
  • Shuffle Python List
  • How to Check if Python List is Empty?
  • Python List of Lists
  • Python List of Dictionaries
  • Python – Convert List to Dictionary

Python Program to find the Largest Number in a List Example 1

The Python max function returns the maximum value in a List.

# Python Program to find Largest Number in a List a = [10, 50, 60, 120, 20, 15] print("The Largest Element in this List is : ", max(a))

Python largest list number output

The Largest Element in this List is : 120

Introduction

In this section of python programming, we will understand the different methods to find the largest number in the list.

How do you find the largest number in a list using for loops in Python?
Smallest and Largest number in list — programminginpython.com

Here we use 2 predefined functions min() and max() which check for the smallest and largest number in a list respectively.

Python program to find largest number in a list

Given a list of numbers, the task is to write a Python program to find the largest number in given list.

Examples:

Input : list1 = [10, 20, 4] Output : 20 Input : list2 = [20, 10, 20, 4, 100] Output : 100

Method 1 : Sort the list in ascending order and print the last element in the list.




# Python program to find largest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the last element
print("Largest element is:", list1[-1])

Output:

Largest element is: 99

Method 2 : Using max() method






# Python program to find largest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# printing the maximum element
print("Largest element is:", max(list1))

Output:

Largest element is: 99

Method 3 : Find max list element on inputs provided by user




# Python program to find largest
# number in a list
# creating empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
# print maximum element
print("Largest element is:", max(list1))

Output:

Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 1 Enter elements: 99 Largest element is: 99

Method 4 : Without using built in functions in python:




# Python program to find largest
# number in a list
def myMax(list1):
# Assume first number in list is largest
# initially and assign it to variable "max"
max = list1[0]
# Now traverse through the list and compare
# each number with "max" value. Whichever is
# largest assign that value to "max'.
for x in list1:
if x > max :
max = x
# after complete traversing the list
# return the "max" value
return max
# Driver code
list1 = [10, 20, 4, 45, 99]
print("Largest element is:", myMax(list1))

Output:

Largest element is: 99

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
Python list-programs
python-list
Practice Tags :
python-list

The objective of this Python post, you will see various Python programs that cover the following:

  • Python program to find largest of n numbers using max
  • Python program to find largest of n numbers without using built-in function

Python program to find largest of n numbers using max

  • Take input number for the length of the list using python input() function.
  • Initialize an empty listlst = [].
  • Read each number in your python program using afor loop.
  • In the for loop append each number to the list.
  • Use built-in python functionmax()to find the largest element in a list.
  • End of the program print the largest number from list.
lst = [] num = int(input('How many numbers: ')) for n in range(num): numbers = int(input('Enter number ')) lst.append(numbers) print("Maximum element in the list is :", max(lst))

Output

How many numbers: 5 Enter number 4 Enter number 6 Enter number 8 Enter number 9 Enter number 10 Maximum element in the list is : 10
Recommended:-Python Program to Calculate n-th term of a Fibonacci Series

Python program to find largest of n numbers without using built-in function

  • Take input number for the length of the list using python input() function.
  • Initialize an empty listlst = [].
  • Read each number in your python program using afor loop.
  • In the for loop append each number to the list.
  • Define a custom function, which is an accepted number list and used to find the largest number from list.
  • Call this custom function and store the function result.
  • End of the program print the largest number from list.
def find_max( list ): max = list[ 0 ] for a in list: if a > max: max = a return max num = int(input('How many numbers: ')) lst = [] for n in range(num): numbers = int(input('Enter number ')) lst.append(numbers) print("Maximum element in the list is :", find_max(lst))

Output

How many numbers: 3 Enter number 1 Enter number 5 Enter number 7 Maximum element in the list is : 7