Python Find the Minimum Value in a List and Its Position

Document 对象参考手册Python3 Examples

In Python, we can use the built-inmin()function to find the minimum value in a list. To find the position of the minimum value, we can use theindex()method. The following is an example code that shows 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 the 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): Use themin()function to find the minimum value in the list, and assign it tomin_value。
  3. min_index = numbers.index(min_value): Use theindex()method to find the position of the minimum value in the list, and assign it tomin_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 对象参考手册Python3 Examples

Other extensions