Find the maximum value python (fixed ver)

This is a modified version of "Let's find the maximum value python" that I raised the other day. The bad points and changes of the previous code are as follows.

Bad points

-When all the elements of the list are negative, the maximum value becomes 0. ・ Processing speed is slow

change point

-Set the initial value of the maximum value candidate to an appropriate element of the list ・ Change from while statement to for statement


I tried refactoring based on two bad points

num = int(input("How many pieces do you want to substitute?"))
value_list = [] #Prepare an array to store numbers

for i in range(num):
    value = int(input("Substitute a number"))
    value_list.append(value) #Add values to the prepared list

max_value = value_list[0] #Initialize with list elements

for i in range(num):
    if value_list[i] > max_value:
        max_value = value_list[i]

print(max_value) #Output maximum value

Changed part

Substitution(Change before)


while i < num:
    value = int(input("Substitute a number"))
    value_list.append(value) #Add values to the prepared list
    i += 1

Substitution(After change)


for i in range(num):
    value = int(input("Substitute a number"))
    value_list.append(value) #Add values to the prepared list

Comparison(Change before)


max_value = 0 #Initialize maximum value to 0
i = 0
while i < num:
    if value_list[i] >= max_value: #Compare the stored numbers in order
        max_value = value_list[i]
    i += 1

Comparison(After change)


max_value = value_list[0] #Initialize with list elements

for i in range(num):
    if value_list[i] > max_value:
        max_value = value_list[i]

By setting the initial value to the input element, the maximum value will be output normally even when all the input elements are negative. Furthermore, by changing from a while statement to a for statement, initialization and addition processing are no longer wasted.

I tried using the max function

I was pointed out in a comment that python has a function to find the maximum value from an object with a given element, so I tried using it immediately.

num = int(input("How many pieces do you want to substitute?"))
value_list = [] #Prepare an array to store numbers

for i in range(num):
    value = int(input("Substitute a number"))
    value_list.append(value) #Add values to the prepared list

print(max(value_list))

Output result

How many pieces do you want to substitute? 3
Substitute a number-3
Substitute a number-2
Substitute a number-5
-2

Thank you to everyone who gave us their opinions and suggestions.

Recommended Posts

Find the maximum value python (fixed ver)
Find the maximum Python
Find the maximum python (improved)
[Python] Find the second smallest value.
Find the mood value with python (Rike Koi)
Find the divisor of the value entered in python
Find the difference in Python
Find the index of the maximum value (minimum value) of a multidimensional array
Find the definition of the value of errno
Extract the maximum value with pandas.
Find the Levenshtein Distance with python
Find the SHA256 value with R (with bonus)
Solve the maximum subarray problem in Python
I used gawk to find out the maximum value that goes into NF.
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
Find the solution of the nth-order equation in python
Find the geometric mean of n! Using Python
[Introduction to Algorithm] Find the shortest path [Python3]
[Python] Find the transposed matrix in a comprehension
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
Find the shortest path with the Python Dijkstra's algorithm
python xlwings: Find the cell in the last row
Find the cumulative distribution function by sorting (Python version)
How to retrieve the nth largest value in Python
Find out the location of Python class definition files.
[Python] Calculate the average value of the pixel value RGB of the object
Find the part that is 575 from Wikipedia in Python
Get the value selected in Selenium Python VBA pull-down
Find the Hermitian matrix and its eigenvalues in Python
python chrome driver ver. Solving the problem of difference
Extract the maximum value with pandas and change that value
the zen of Python
List find in Python
[Python] Split the date
I tried to find the entropy of the image with python
I want to initialize if the value is empty (python)
Find out the apparent width of a string in python
[Python] Heron's formula functionalization and calculation of the maximum area
Inherit the standard library to find the average value of Queue
Write a python program to find the editing distance [python] [Levenshtein distance]
Creating a Python script that supports the e-Stat API (ver.2)
Python --Find out number of groups in the regex expression
Get the value while specifying the default value from dict in Python
How to get the last (last) value in a list in Python
Find the diameter of the graph by breadth-first search (Python memory)
Find the eigenvalues of a real symmetric matrix in Python
Extract the value closest to a value from a Python list element