What is the name of the function that returns the highest value in a list?

Find Maximum Value in a List in Python

Python Python List

Created: November-16, 2020 | Updated: December-10, 2020

This tutorial will demonstrate how to find the maximum value in a list in Python.

A few scenarios and data types will be covered, from a simple integer list to a more complex structure such as an array within an array.

Python List max() Method

Advertisements

Previous Page
Next Page

Find Maximum Value in List in Python

In this article, we will learn to find the maximum value in a list in Python. We will use some built-in functions, simple approaches, and some custom codes as well to understand the logic. Let's first have a quick look over what is a list in Python and how can we find the maximum value or largest number in a list.

Python max()

In this tutorial, we will learn about the Python max() function with the help of examples.

The max() function returns the largest item in an iterable. It can also be used to find the largest item between two or more parameters.

Example

numbers = [9, 34, 11, -4, 27]
# find the maximum number max_number = max(numbers)
print(max_number) # Output: 34

The max() function has two forms:

# to find the largest item in an iterable max(iterable, *iterables, key, default) # to find the largest item between two or more objects max(arg1, arg2, *args, key)

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
Read Full Article

MAX function in Excel: formula examples to find and highlight highest value

by | updated on March 2, 2021

The tutorial explains the MAX function with many formula examples that show how to find highest value in Excel and highlight largest number in your worksheet.

MAX is one of the most straightforward and easy-to-use Excel functions. However, it does have a couple of tricks knowing which will give you a big advantage. Say, how do you use the MAX function with conditions? Or how would you extract the absolute largest value? This tutorial provides more than one solution for these and other related tasks.

  • MAX function in Excel
  • How to make a MAX formula
  • How to use MAX function in Excel – formula examples
    • Find max value in a group
    • Max formula for non-adjacent cells or ranges
    • Find max date in Excel
    • Conditional MAX in Excel
    • Find max value ignoring zeros
    • Get largest number ignoring errors
    • Find absolute maximum value
  • How to highlight max value in Excel
  • Excel MAX function not working

Python max() Function

❮ Built-in Functions

Example

Return the largest number:

x = max(5, 10)
Try it Yourself »

Video liên quan

Postingan terbaru

LIHAT SEMUA