Python min() Function (2024)

Last Updated : 03 Jul, 2024

Summarize

Comments

Improve

Python min() function returns the smallest of the values or the smallest item in an iterable passed as its parameter.

Example: Find Python min integer from the list

Python
numbers = [23,25,65,21,98]print(min(numbers))

Output

21

Python min() Function Syntax

min(a, b, c, …, key=func)

The min() function in Python can take any type of object of similar type and return the smallest among them. In the case of strings, it returns lexicographically the smallest value.

Parameters

  • a, b, c, .. : similar type of data.
  • key (optional): A function to customize the sort order

Return

Returns the smallest item.

What is min() Function in Python?

Python min() function is used to find the minimum value. It is the opposite function of max().

You can use this function on any iterable like a string, list, tuple, etc. which makes it very useful.

How to use min() function in Python?

Using min() function in Python is very easy. You just need to pass the list as a parameter in the min function and it will return the minimum number.

Example: We can easily find Python min of two numbers.

Python
print(min(45,56))

Output

45

More Python min() Examples

In this example, we find the minimum element in different reference using Python min().

Example 1: Find Python Min of List

In this example, we are using min() to locate the smallest item in Python in a list.

Python
numbers = [3, 2, 8, 5, 10, 6]small = min(numbers);print("The smallest number is:", small)

Output

The smallest number is: 2

Example 2: Find min in List of String

In this example, we are using min() to locate the smallest string in Python in a list.

Python
languages = ["Python", "C Programming", "Java", "JavaScript",'PHP','Kotlin']small = min(languages)print("The smallest string is:", small)

Output

The smallest string is: C Programming

Example 3: Minimum Element in a Dictionary

In this example, we are using min() to find the minimum element in a dictionary.

Python
square = {5: 25, 8: 64, 2: 4, 3: 9, -1: 1, -2: 4}print("The smallest key:", min(square)) # -2key2 = min(square, key = lambda k: square[k])print("The smallest value:", square[key2]) # 1

Output

The smallest key: -2The smallest value: 1

In this article, we discussed the definition, syntax, and examples of the Python min() function. min() function in Python is very versatile and can be used with any iterable.

Hope this article helped you understand how to use the min() function, and you can effectively use it in your projects.

Read More Python Built-in Functions

Similar Reads:

  • Find Min/Max in heterogeneous list
  • max() and min() in Python
  • Use of min() and max() in Python

Python min() Function – FAQs

How does min() handle different data types like integers, floats, and strings?

The min() function can compare and find the minimum value among elements of the same type:

  • Integers and floats: It compares numerical values directly.
  • Strings: It compares strings lexicographically (dictionary order).
print(min(3, 1.5, 2)) # Output: 1.5
print(min("apple", "banana", "cherry")) # Output: "apple"

What happens if we apply min() to an empty sequence?

If you apply min() to an empty sequence without specifying a default value, it raises a ValueError.

print(min([])) # Raises ValueError: min() arg is an empty sequence

You can avoid this by providing a default value using the default keyword argument.

print(min([], default="No elements")) # Output: "No elements"

How does min() handle sequences with duplicate minimum values?

If a sequence contains duplicate minimum values, min() returns the first occurrence of the minimum value.

print(min([2, 3, 1, 4, 1])) # Output: 1 (first occurrence)

How to use min() with tuples and sets?

You can use min() with tuples and sets just as you would with lists.

# Tuples
print(min((5, 3, 9, 1))) # Output: 1

# Sets
print(min({7, 2, 8, 3})) # Output: 2

Can we use min() with custom objects and classes?

Yes, you can use min() with custom objects and classes, but you need to define how the objects should be compared. This is done by implementing comparison methods like __lt__ (less than) in your class.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __lt__(self, other):
return self.age < other.age

def __repr__(self):
return f"{self.name} ({self.age})"

p1 = Person("Alice", 30)
p2 = Person("Bob", 25)
p3 = Person("Charlie", 35)

print(min(p1, p2, p3)) # Output: Bob (25)

In this example, min() uses the __lt__ method to compare the Person objects based on their age.



N

nikhilaggarwal3

Python min() Function (1)

Improve

Previous Article

Python List max() Method

Next Article

How To Find the Length of a List in Python

Please Login to comment...

Python min() Function (2024)
Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 5634

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.