JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Get the index of the maximum and minimum values in a list in Python

Author:JIYIK Last Updated:2025/05/09 Views:

In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python.


max()Get the index of the maximum value in a list using and list.index()functions in Python

max()The function gives the maximum value in a Python list. list.index(x)The method gives xthe index of in a list. The following code example shows us how to get the index of the maximum value of a list using the max()and list.index()functions in Python.

list1 = [10, 12, 13, 0, 14]

tmp = max(list1)
index = list1.index(tmp)

print(index)

Output:

4

In the code above, we first use max()the function to get list1the maximum value in the list and store it in tmp, and then get the index of the maximum value by tmppassing to list1.index()the method. If we only want to display the index of the maximum value, the above code can be shortened.

list1 = [10, 12, 13, 0, 14]

print(list1.index(max(list1)))

Output:

4

min()Get the index of the minimum value in a list using and list.index()functions in Python

min()The function gives the minimum value in a Python list. list.index(x)The method has been discussed in the previous section. The following code example shows us how to get the index of the minimum value of a list using the min()and functions in Python.list.index()

list1 = [10, 12, 13, 0, 14]

tmp = min(list1)
index = list1.index(tmp)

print(index)

Output:

3

In the above code, we first use min()the function to get list1the minimum value within the list and store it in tmp, and then tmppass to list1.index()the function. The above code can be shortened if we only want to display the index of the minimum value.

list1 = [10, 12, 13, 0, 14]

print(list1.index(min(list1)))

Output:

3

numpy.argmax()Get the index of the maximum value of a list using the function in Python

NumPyThe get_maximum_value function from the package numpy.argmax()provides us with the index of the maximum value in a list or array as an argument passed to the function as a parameter. The following code example shows us how to numpy.argmax()get the index of the maximum value of a list using the get_maximum_value function in Python.

import numpy

list1 = [10, 12, 13, 0, 14]
maxindex = numpy.argmax(list1)

print(maxindex)

Output:

4

In the code above, we use numpy.argmax()the function to get list1the index of the maximum value in the list .


Use the function in Python numpy.argmin()to get the index of the minimum value of a list

NumPyThe function in the package numpy.argmin()provides us with a list or array to pass as an argument to the function. The following code example shows us how to numpy.argmin()get the index of the minimum value of a list using the function in Python.

import numpy

list1 = [10, 12, 13, 0, 14]
minindex = numpy.argmin(list1)

print(minindex)

Output:

3

In the code above, we use numpy.argmin()the function to get list1the index of the minimum value in the list .

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding a string in a list in Python

Publish Date:2025/05/09 Views:75 Category:Python

This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem

Getting list shape in Python

Publish Date:2025/05/09 Views:139 Category:Python

In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim

Adding multiple elements to a list in Python

Publish Date:2025/05/09 Views:180 Category:Python

List is a mutable data structure in Python. It can contain values ​​of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi

How to Find the Maximum Value in a List in Python

Publish Date:2025/05/09 Views:99 Category:Python

This tutorial will demonstrate how to find the maximum value in a list in Python. This tutorial will cover a number of scenarios and data types, from simple lists of integers to more complex structures like arrays within arrays. for Finding

Convert a list to a set in Python

Publish Date:2025/05/08 Views:145 Category:Python

This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values ​​in sequence. Python list stores comma separated values ​​between

Remove the first element from a list in Python

Publish Date:2025/05/08 Views:173 Category:Python

This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which

Convert a list to lowercase in Python

Publish Date:2025/05/08 Views:112 Category:Python

Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin

Remove all occurrences of an element from a Python list

Publish Date:2025/05/08 Views:69 Category:Python

In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element

Convert hex to bytes in Python

Publish Date:2025/05/08 Views:102 Category:Python

Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial