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:
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.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.
Share this:
- More
- Tumblr
- Telegram
- Skype
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.
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)RunOutput
Largest element is: 52a[-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)RunOutput
Largest element is: 52In 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.
Related Tutorials
- 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
Introduction
In this section of python programming, we will understand the different methods to find the largest number in the list.