Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Find the Minimum Value in a List and Its Position

Python Find the Minimum Value in a List and Its Position

Document Object Reference Manual Python3 Examples

In Python, we can use the built-in min() function to find the minimum value in the list. To find the position of the minimum value, we can use the index() method. The following is an example showing how to find the minimum value in a list and its position.

Example

Define a list

numbers

=

[

34

,

12

,

56

,

78

,

23

,

9

]

Find the minimum value

min_value

=

min

(

numbers

)

Find the position of the minimum value

min_index

=

numbers.

index

(

min_value

)

Output result

print

(

f

"The minimum value in the list is: {min_value}"

)

print

(

f

"The position of the minimum value is: {min_index}"

)

Code analysis:

  1. numbers = [34, 12, 56, 78, 23, 9]: Define a list containing multiple integers.
  2. min_value = min(numbers): When using min() function to find the minimum value in the list and assign it to min_value。
  3. min_index = numbers.index(min_value): When using index() method to find the position of the minimum value in the list and assign it to min_index。
  4. print(f"列表中的最小值是: {min_value}"): Output the minimum value.
  5. print(f"最小值的位置是: {min_index}"): Output the position of the minimum value.

Output result:

列表中的最小值是: 9
最小值的位置是: 5

Document Object Reference Manual Python3 Examples