Python List min() Method



The Python list min() method compares the elements of the list and returns the element with minimum value.

If the elements in the list are numbers, the comparison is done numerically; but if the list contains strings, the comparison is done alphabetically.

Note: The comparison in this method is only performed among same data types. If the list contains elements of multiple types, a TypeError is raised.

Syntax

Following is the syntax for the Python List min() method −

min(list)

Parameters

  • list − This is a list from which min valued element to be returned.

Return Value

This method returns the elements from the list with minimum value.

Example

The following example shows the usage of the Python List min() method. The method performs alphabetical comparison on the string elements of the list created.

list1 = ['123', 'xyz', 'zara', 'abc']
print("min value element : ", min(list1))

When we run above program, it produces following result −

min value element :  123

Example

Now let us find the minimum number in a list containing only numbers in the example below.

list2 = [456, 700, 200]
print("min value element : ", min(list2))

If we compile and run the given program, the output is displayed as follows −

min value element :  200

Example

If the list contains multiple data type elements, the method fails to retrieve a minimum valued element as the comparison is not possible between two different data types.

list1 = [123, 'xyz', 'abc', 456]
print("min value element : ", str(min(list1)))

Once we compile and run the program above, the TypeError is raised.

Traceback (most recent call last):File "main.py", line 2, in <module>print("min value element : ", str(min(list1)))TypeError: '<' not supported between instances of 'str' and 'int'
python_lists.htm
Advertisements