• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python min() function – Get Minimum of List or Dictionary with min() Function

Python min() function – Get Minimum of List or Dictionary with min() Function

February 7, 2022 Leave a Comment

To get the minimum of a list of numbers or strings in Python, we can use the Python min() function. You can use the min() function in Python to get the minimum value of numbers in a list, strings in a list, and values in a dictionary.

list_of_numbers = [10,32,98,38,47,34]

print(min(list_of_numbers))

#Output
10

There are many powerful built-in functions in the Python language which allow us to perform basic or complex tasks easily.

One such task is to find the minimum value of a collection of numbers or strings.

We can use the Python min() function to find the minimum, or smallest, value in an iterable (list, dictionary, set, etc.) made up of numbers or strings.

Let’s say we have the following list of numbers in Python. We can use the Python built-in min() function to get the minimum value of the list in the following way:

list_of_numbers = [10,32,98,38,47,34]

print(min(list_of_numbers))

#Output
10

We can also call the min() function with multiple arguments. In this case, min() returns the smallest value of the arguments.

print(min(10,32,98,38,47,34))

#Output
10

For a collection of strings, the smallest value is determined after sorting alphabetically.

print(min("Mike","Alex","Patricia"))

#Output
Abby

If you instead want to find the maximum value of an iterable, you can use the Python max() function.

If you are using pandas in Python and want to get the minimum value of a column or DataFrame, you can use the pandas min() function.

How to Find Minimum of Two Numbers Using Python min() Function

In Python, we can easily find the minimum of two numbers using the min() function.

Below are a few examples of getting the min value between two numbers using min().

print(min(10,32))
print(min(-30,4))
print(min(40,-21))
print(min(8,-22))
print(min(98,99))

#Output
10
-30
-21
-22
98

How to Find Smallest Value in List of Numbers Using Python min() Function

We can use the Python min() function to get the smallest value in a list of numbers easily. To find the min value of a list, we just pass the list to min().

Let’s say we have the following list of numbers.

list_of_numbers = [10,32,98,38,47,34]

To get the minimum value of a list of numbers in Python, we use the min() function.

list_of_numbers = [10,32,98,38,47,34]

print(min(list_of_numbers))

#Output
10

How to Get Minimum Value in List of Strings Using Python min() Function

We can use the Python min() function to get the smallest value in a list of strings easily. To find the min value of a list, we just pass the list to min().

The smallest value from a list of strings is the first string in alphabetical order from A to Z, meaning the string that starts with a letter closest to A will be the smallest. Also, note that uppercase letters come before lowercase letters.

Let’s say we have the following list of strings.

list_of_strings = ["Low","High","Better","Worse"]

To get the minimum value of a list of strings in Python, we use the min() function.

list_of_strings = ["Low","High","Better","Worse"]

print(min(list_of_strings))

#Output
Low

How to Find Minimum Value in Dictionary Using Python min() Function

With the Python min() function, we can find the minimum value in a dictionary.

Let’s say we have the following dictionary.

dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }

To get the min value in a dictionary, we pass the dictionary to min() as shown in the following Python code.

dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }

print(min(dict))

#Output:
-3

We can also get the minimum value of the keys in a dictionary. To do so, we need to pass a lambda expression for min() as the “key”.

Below is how to find the min value of the keys in a dictionary in Python.

dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }

print(min(dict, key = lambda k: dict[k]))

#Output:
Alex

Hopefully this article has been useful for you to learn how to use the Python min() function to get the minimum value of a list of numbers or strings.

Other Articles You'll Also Like:

  • 1.  How to Group By Columns and Find Standard Deviation in pandas
  • 2.  Using Python to Convert Integer to String with Leading Zeros
  • 3.  Using Lambda Expression with min() in Python
  • 4.  How to Filter pandas DataFrame by Date
  • 5.  Symmetric Difference of Two Sets in Python
  • 6.  Writing a Table Faster to Word Document Using python-docx
  • 7.  Check if Class is Subclass in Python with issubclass()
  • 8.  Python Increment Counter with += Increment Operator
  • 9.  Using Python to Print Variable Type
  • 10.  Using Python to Convert Decimal to String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy