Use of min() and max() in Python Last Updated : 06 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively, when we passed string or list of strings as arguments. python3 l= ["ab", "abc", "abd", "b"] l1="abc" # prints 'b' print(max(l)) # prints 'ab' print(min(l)) #prints 'c' print(max(l1)) #prints 'a' print(min(l1)) Here you notice that output comes according to lexicographical order. but we can also find output according to length of string or as our requirement by simply passing function name or lambda expression. Parameters: By default, min() and max() doesn't require any extra parameters. however, it has an optional parameters: key - function that serves as a key for the min/max comparison Syntax: max(x1, x2, ..., xn, key=function_name)here x1, x2, x3.., xn passed arguments function_name : denotes which type of operation you want to perform on these arguments. Let function_name=len, so now output gives according to length of x1, x2.., xn. Return value: It returns a maximum or minimum of list according to the passed parameter. Python3 # Python code explaining min() and max() l = ["ab", "abc", "bc", "c"] print(max(l, key = len)) print(min(l, key = len)) Output: abc c Explanation: In the above program, max() function takes two arguments: l(list) and key = len(function_name).this key = len(function_name) function transforms each element before comparing, it takes the value and returns 1 value which is then used within max() or min() instead of the original value.Here key convert each element of the list to its length and then compare each element according to its length. initially l = ["ab", "abc", "bc", "c"] when we passed key=len as arguments then it works like l=[2,3,2,1] Python3 # Python code explaining min() and max() def fun(element): return(len(element)) l =["ab", "abc", "bc", "c"] print(max(l, key = fun)) # you can also write in this form print(max(l, key = lambda element:len(element))) Output: abc abc Another example: Python3 # Python code explaining min() and max() l = [{'name':'ramu', 'score':90, 'age':24}, {'name':'golu', 'score':70, 'age':19}] # here anonymous function takes item as an argument. print(max(l, key = lambda item:item.get('age'))) Output: {'age': 24, 'score': 90, 'name': 'ramu'} Similar we can use min() function instead of max() function as per requirement. Comment More infoAdvertise with us Next Article Use of min() and max() in Python rrlinus Follow Improve Article Tags : Python Python function-programs Practice Tags : python Similar Reads max() and min() in Python This article brings you a very interesting and lesser-known function of Python, namely max() and min(). Now when compared to their C++ counterpart, which only allows two arguments, that too strictly being float, int or char, these functions are not only limited to 2 elements, but can hold many eleme 3 min read sys.maxint in Python In Python 2, sys.maxint refers to the maximum value an integer can hold on a given platform. This value is:2³¹ - 1 = 2147483647 on a 32-bit system2â¶Â³ - 1 = 9223372036854775807 on a 64-bit systemsys.maxint was typically used as a very large number in algorithms requiring an upper bound, like finding 3 min read Find Maximum of two numbers in Python Finding the maximum of two numbers in Python helps determine the larger of the two values. For example, given two numbers a = 7 and b = 3, you may want to extract the larger number, which in this case is 7. Let's explore different ways to do this efficiently.Using max()max() function is the most opt 2 min read Maximum and Minimum element in a Set in Python We are given a set and our task is to find the maximum and minimum elements from the given set. For example, if we have a = {3, 1, 7, 5, 9}, the maximum element should be 9, and the minimum element should be 1.Letâs explore different methods to do it:1. Using min() & max() functionThe built-in m 3 min read Python nmaxmin Module A nmaxmin module is that library of Python which helps you to find the nth maximum number and nth minimum number in a given list. It directly returns either the maximum number or minimum number with respect to the given index (n). Here the given list must not be in sorted order as it will traverse t 2 min read Python - Find maximum value in each sublist Finding the maximum value in each sublist involves iterating through a list of lists and identifying the largest element within each sublist. This operation is useful for analyzing data sets, and extracting the most significant values from grouped information. In this article, we will explore method 2 min read Python | Max/Min value in Nth Column in Matrix Sometimes, while working with Python Matrix, we may have a problem in which we require to find the minimum and maximum value of a particular column. This can have a possible application in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed 7 min read List Methods in Python | Set 1 (in, not in, len(), min(), max()...) List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3 2 min read Precision Handling in Python Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the "math" module. In this article, we will use high-precision calculations in Python with Decimal in Python.ExampleInput: x = 2.4Output: Integra 6 min read SymPy | Permutation.min() in Python Permutation.min() : min() is a sympy Python library function that returns the minimum value in the permutation. Syntax : sympy.combinatorics.permutations.Permutation.min() Return : minimum value in the permutation Code #1 : min() Example Python3 1=1 # Python code explaining # SymPy.Permutation.min() 1 min read Like