You are currently viewing Python List min() Function

The min() is a built-in function of Python that is used to find the smallest element in a list, tuple, or any other iterable object. To find the smallest element in a list, use the min() function with the list as its argument. This function returns the value of the smallest element in the list. In this article, I will explain the Python list min() method and using its syntax, parameters, and usage how we can find the minimum element from a given list with examples.

Advertisements

1. Quick Examples of List min() Function

If you are in a hurry, below are some quick examples of the list min() function.


# Quick examples of list min() method

# Example 1: Get the smallest element 
# in a list using min() Function
mylist = [25, 53, 12, 66, 79]
min_value = min(mylist)

# Example 2: Find the smallest element in a tuple 
mytuple = (25, 53, 12, 66, 79)
min_value = min(mytuple)

# Example 3: Find the smallest element in a set 
myset = {25, 53, 12, 66, 79}
min_value = min(myset)

# Example 4: Get the smallest element
# among multiple arguments using min() 
x = 20
y = 10
z = 30
smallest = min(x, y, z)

# Example 5: Get the smallest element 
# Alphabetically from a list of strings
mylist =['Python','Spark','C++','Hadoop']
min_value = min(mylist)

# Example 6: Sort the list in ascending order
# get the smallest element
mylist = [25, 53, 12, 66, 79]
mylist.sort() 
result = mylist[0] 

# Example 7: Sort the list in descending order
# get the smallest
mylist.sort(reverse=True) 
result = mylist[-1] 

# Example 8: Get the smallest element 
# Using the lambda function
mylist = [25, 53, 12, 66, 79]
result = min(mylist, key=lambda value: int(value))

# Example 9: Get the smallest element 
# Using reduce function
mylist = [25, 53, 12, 66, 79]
min_value = reduce(min, mylist) 

# Example 10: Get the smallest element 
# Using the enumerate function
min_value,index = min((min_value,index) for (index,min_value) in enumerate(mylist))

# Example 11: Finding minimum value
# Using numpy module
mylist = [25, 53, 12, 66, 79]
min_value = np.min(mylist) 

2. List min() Function

To find the smallest item in a list or other iterable object like a tuple or set, you can use the min() function in Python. This function takes the list as an argument and returns the smallest item. If the items in the list are strings, they will be compared alphabetically.

2.1 Syntax of List min() Function

Following is the syntax of the list min() function


# Syntax of min() function
min(list)

2.2 Parameter of List min()

  • list – This is a list from which we have to find the min valued element to be returned.

2.3 Return Value

This function returns the value of the smallest element in the list.

3. Get the Smallest Element Using min() Function

You can use the min() function in Python to get the minimum value from a list or an iterable. For example, the min() takes the list named mylist as an argument and returns the smallest number in the list, which is 12.


# Initialize list
mylist = [25, 53, 12, 66, 79]
print("Original list: ", mylist)

# Get smallest element
# Using min() Function
min_value = min(mylist)
print("Get smallest element is: ", min_value)

Yields below output.

python list min

The min() function can also be used with other iterable types, such as tuples or sets.


# Initialize tuple
mytuple = (25, 53, 12, 66, 79)
print("Original tuple: ", mytuple)

# Find the tuple minmum value
min_value = min(mytuple)
print("Get smallest element is: ", min_value)

# Output:
# Original tuple:  (25, 53, 12, 66, 79)
# Get smallest element is:  12

# Initialize set
myset = {25, 53, 12, 66, 79}
print("Original set: ", myset)

# Find the set minimum value
min_value = min(myset)
print("Get smallest element is: ", min_value)

# Output:
# Original set:  {25, 53, 12, 66, 79}
# Get smallest element is:  12

You can also use the min() function with multiple arguments to find the smallest element among them. For example, you have three variables x, y, and z, which have the values 20, 10, and 30, respectively. you can use the min() function with these variables as arguments to find the smallest value among them, which is 10.


# Get smallest element
# Using multiple arguments 
x = 20
y = 10
z = 30
smallest = min(x, y, z)
print("Get smallest element is: ",smallest)

# Output: 
# Get smallest element is: 10

4. Get the Smallest Element Alphabetically from a List

You can also use the min() function to get the smallest element alphabetically from a list of strings. For example, you have a list of strings called mylist. You can use the min() function to get the smallest element alphabetically, which is “C++“.

Note that the min() function compares elements alphabetically based on their ASCII values, so uppercase letters will be considered smaller than lowercase letters.


# Initialize list
mylist =['Python','Spark','C++','Hadoop']
print("Original list: ", mylist)

# Get the smallest element 
# Alphabetically from a list of strings
min_value = min(mylist)
print("Get smallest element is: ", min_value)

Yields below output.

python list min

5. Get the Smallest Element Using sort() Function

You can also get the smallest element in a list using the sort() function in Python, you can sort the list in ascending order using sort() and then access the first element of the sorted list. For example, the sort() function is used to sort the mylist in ascending order. Then, the first element of the sorted list, which is the smallest element, is accessed using indexing and assigned to the variable result.


# Initialize list
mylist = [25, 53, 12, 66, 79]
print("Original list: ", mylist)

# Sort in ascending order
mylist.sort() 
# get the first element
result = mylist[0] 
print("Get smallest element is: ", result)

# Output:
# Original list: [25, 53, 12, 66, 79]
# Get smallest element is: 12

You can also sort the list in descending order using the sort() function with the reverse parameter set to True, and finally retrieves the smallest element by accessing the last element of the sorted list using the index -1.


# Sort in descending order
mylist.sort(reverse=True) 
# get the first element
result = mylist[-1] 
print("Get smallest element is: ", result)

Yields the same output as above.

6. Get the Smallest Element Using the Lambda Function

You can use the min() function with a lambda function to retrieve the smallest element from the list based on the integer value of each element. The lambda function takes a value parameter and returns the integer value of value using the int() function.


# Initialize list
mylist = [25, 53, 12, 66, 79]
print("Original list: ", mylist)

# Get the smallest element 
# Using the lambda function
result = min(mylist, key=lambda value: int(value)) 
print("Get smallest element is: ", result)

Yields the same output as above.

7. Get the Smallest Element Using reduce Function

You can use the reduce() is a very useful function in functional programming and can be used in Python as well. However, as you mentioned, it has been moved to the functools module.

In the below example, first, create a list of numbers called mylist. Use the reduce() function to find the minimum value in the list by passing the min function as the first argument and the mylist as the second argument. Assign the result to a variable called min_value.


from functools import reduce

# Initialize list
mylist = [25, 53, 12, 66, 79]
print("Original list: ", mylist)

# Get the smallest element 
# Using reduce function
min_value = reduce(min, mylist) 
print("Get smallest element is: ", min_value)

# Output:
# Original list: [25, 53, 12, 66, 79]
# Get smallest element is: 12

8. Get the Smallest Element Using the enumerate Function

You can also use the min() function along with the enumerate() function to find the smallest element in a list. The enumerate() function takes an iterable as input and returns an iterator that yields tuples containing the index and the value of each element in the iterable.

In the below example, a generator expression is used inside the min() function. The generator expression yields tuples of (min_value, index) pairs for each element in the list(mylist). The min() function then finds the tuple with the smallest min_value and returns it.


# Initialize list
mylist = [25, 53, 12, 66, 79]
print("Original list: ", mylist)

# Get the smallest element 
# Using the enumerate function
min_value,index = min((min_value,index) for (index,min_value) in enumerate(mylist))
print("Get smallest element is: ", min_value)

Yields the same output as above.

9. Using NumPy Module

The NumPy module provides the numpy.min() function that can be used to find the smallest element in a list or array. In the below example, you first import the numpy module and define a list called mylist. Then, you use the np.min() function to find the smallest element in the list and store it in the min_value variable.


import numpy as np
 
#Initializing list
mylist = [25, 53, 12, 66, 79]
print("Original list: ", mylist)
 
# finding minimum value
# Using numpy module
min_value = np.min(mylist) 
print("Get smallest element is: ", min_value)

Yields the same output as above.

10. Conclusion

In this article, I have explained the Python list min() function and using this syntax, parameters, and usage how to find the minimum element from a given list with examples.

Happy Learning !!