Minimum element in array python

Python – Find Min Value and its index in List

In this tutorial, we will look at how to find the min value in a Python list and its corresponding index with the help of some examples.

How to get the minimum value in a list in Python?

A simple approach is to iterate through the list and keep track of the minimum value. Alternatively, you can also use the Python built-in min() function to find the minimum value in a list.

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

minimum value in a list

Let’s look at some of the different ways of finding the smallest value and its index in a list.

Loop through the list to find the minimum

Iterate through the list values and keep track of the min value. Here’s an example.

# create a list ls = [3, 6, 7, 2, 1, 5] # find min value using loop min_val = ls[0] for val in ls: if val < min_val: min_val = val # display the min value print(min_val)

Here, we iterate over each value in the list ls and keep track of the minimum value encountered in the variable min_val . After the loop finishes, the variable min_val stores the minimum value present in the list, 1.

You can use this method to get the index corresponding to the minimum value in the list as well. Use an additional variable to keep track of the current minimum value’s index.

# create a list ls = [3, 6, 7, 2, 1, 5] # find min value using loop min_val = ls[0] min_val_idx = 0 for i in range(len(ls)): if ls[i] < min_val: min_val = ls[i] min_val_idx = i # display the min value print(min_val) # display its index print(min_val_idx)

We get the minimum value and its index after the loop finishes. Here we iterate through the list via its index rather than the values. You can also use the enumerate() function to iterate through the index and value together.

Читайте также:  Python two dicts to one

Using min() to get the maximum value

You can also use the Python built-in min() function to get the min value in a list. The function returns the minimum value in the passed iterable (for example, list, tuple, etc.).

# create a list ls = [3, 6, 7, 2, 1, 5] # find min value min(ls)

Using the min() function is simple and is just a single line code compared to the previous example.

You can use the list index() function to find the index corresponding to the minimum value (assuming you already know the minimum value).

# create a list ls = [3, 6, 7, 2, 1, 5] # find min value min_val = min(ls) # display the min value print(min_val) # display its index print(ls.index(min_val))

We get the min value and its index in the list ls .

Note that the list index() function returns the index of the first occurrence of the passed value. If the min value occurs more than once in the list, you’ll only get the index of its first occurrence. You can use list comprehension to get all the indices of occurrence of the min value in the list.

# create a list ls = [3, 6, 1, 2, 1, 5] # find min value min_val = min(ls) print(min_val) # find all indices corresponding to min val min_val_idx = [i for i in range(len(ls)) if ls[i]==min_val] print(min_val_idx)

We get all the indices where the minimum value occurs in the list ls .

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Читайте также:  Python чтение строки до определенного символа

Источник

Find Index of Minimum Element in a List in Python

Find Index of Minimum Element in a List in Python

  1. Use the min() and index() Functions to Find the Index of the Minimum Element in a List in Python
  2. Use the min() Function and for Loop to Find the Index of the Minimum Element in a List in Python
  3. Use the min() and enumerate() Functions to Find the Index of the Minimum Element in a List in Python
  4. Use the min() and operator.itemgetter() Functions to Find the Index of the Minimum Element in a List in Python
  5. Use the min() and __getitem__() Functions to Find the Index of the Minimum Element in a List in Python
  6. Use the numpy.argmin() Function to Find the Index of the Minimum Element in a List in Python
  7. Conclusion

A list object in Python emulates an array and stores different elements under a common name. Elements are stored at a particular index which we can use to access them.

We can perform different operations with a list. It is straightforward to use built-in list functions like max() , min() , len , and more to return the maximum element, smallest element, and list length.

This article will find the minimum element index in a Python list.

Use the min() and index() Functions to Find the Index of the Minimum Element in a List in Python

In Python, we can use the min() function to find the smallest item in the iterable. Then, the index() function of the list can return the index of any given element in the list.

A ValueError is raised if the given element is not in the list.

lst = [8,6,9,-1,2,0] m = min(lst) print(lst.index(m)) 

Remember, the index of a list starts from 0. The above answer shows 3 since the smallest element is in the fourth position.

Use the min() Function and for Loop to Find the Index of the Minimum Element in a List in Python

We can substitute the use of the index() function in the previous method with a for loop. It can iterate over the list and compare elements individually.

When there is a match, we return the value of that index and break out of the loop using the break statement.

lst = [8,6,9,-1,2,0] m = min(lst) for i in range(len(lst)):  if(lst[i]==m):  print(i)  break 

Use the min() and enumerate() Functions to Find the Index of the Minimum Element in a List in Python

The enumerate() function accepts an iterable. It returns an object containing the elements of the iterable with a counter variable for the element index.

Читайте также:  Decode from base64 javascript

This object can be iterated using a for loop. Then, we will iterate over this object using list comprehension, create a new list, and use the min() function to locate the minimum element in a list.

We will get the element and its index in one line.

lst = [8,6,9,-1,2,0] a,i = min((a,i) for (i,a) in enumerate(lst)) print(i) 

Use the min() and operator.itemgetter() Functions to Find the Index of the Minimum Element in a List in Python

The operator module in Python provides additional operators which we can use to simplify our code. The itemgetter() function from this module returns a callable object and can retrieve some element from its operand.

The min() function accepts a key parameter to determine the criteria for the comparison. We can provide the itemgetter() function as the value for this parameter to return the index of the minimum element.

from operator import itemgetter lst = [8,6,9,-1,2,0] i = min(enumerate(lst), key=itemgetter(1))[0] print(i) 

We first find the minimum element and its index in the previous methods. This method does both these steps in one line; therefore, it is considered a faster approach.

Use the min() and __getitem__() Functions to Find the Index of the Minimum Element in a List in Python

The operator.itemgetter() function calls the magic function __getitem__() internally. We can avoid the need for importing the operator module by directly working with this function and improving the speed of the code.

It is similar to the previous method to return the minimum element index in a list in Python.

lst = [8,6,9,-1,2,0] i = min(range(len(lst)), key=lst.__getitem__) print(i) 

Use the numpy.argmin() Function to Find the Index of the Minimum Element in a List in Python

The numpy.argmin() function is used to find the position of the smallest element in Python. We can use this function with lists, and it will return an array with the indices of the minimum element of the list.

import numpy as np lst = [8,6,9,-1,2,0] i = np.argmin(lst) print(i) 

Conclusion

To wrap up, we discussed several methods to find the index of the minimum element in a list. The min() function was the most common among all the methods.

Different functions like enumerate() , itemgetter() , and more can be used to create different approaches. The final method, using the numpy.argmin() function, is more straightforward.

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

Related Article - Python List

Источник

Оцените статью