Usually, you could just use
max(nums)
If you explicitly want to use a loop, try:
max_value = None
for n in nums:
if max_value is None or n > max_value: max_value = n
Answer from helmbert on Stack OverflowUsually, you could just use
max(nums)
If you explicitly want to use a loop, try:
max_value = None
for n in nums:
if max_value is None or n > max_value: max_value = n
Here you go...
nums = [14, 8, 9, 16, 3, 11, 5]
big = max(nums)
spot = nums.index(big)
This would be the Pythonic way of achieving this. If you want to use a loop, then loop with the current max value and check if each element is larger, and if so, assign to the current max.
lst = [1,2,3,4,5]
basically, I want to find max: max(lst)
but I want to use a syntax like this: var = max(item) for item in lst
first, why does this return a generator object instead of throwing some error?
And second, how to do it properly?
Edit: so basically there is a function. I need to compute the max value returned by the function. The function should calculate values from a list of inputs.
So, I need to de this:
var = max(func(item)) for item in lst
I just wanted to know if we can do this in one or two liner?
Python- Finding the largest number in a list using forloop or while loop - Stack Overflow
python - Find the greatest (largest, maximum) number in a list of numbers - Stack Overflow
for loop - How to find the index of the max value in a list for Python? - Stack Overflow
How to calculate min/max values in a list without using the min()/max() functions?
Videos
there's also a built in function called max... works like a charm
This question was asked 9 years ago but I'm giving my answer because the question is still relevant today
we can do this for both numbers and strings
A) Finding the largest number in a given list:
your_list = [54, 26, 29, 48, 56, 32, 15, 17]
largest_num = -99999999 # Any value below zero is ok if you know there
# are larger numbers in the list
for i in your_list: # Checking every item in the list
print("current number:", i) # Printing every item in the list
# regardless of their value
if i > largest_num: # Is it larger than -99999999?!
largest_num = i # then value of largest number should be equal
# to the value of i(as integer)
print("largest number:",largest_num) # Let's print the result when
# we're done
B) Finding the largest string in a given list:
my_list = ["a", "b", "c", "A", "B", "C", " "]
largest_str = my_list[0]
for i in my_list:
print("current str:", i)
if i > largest_str:
largest_str = i
print("largest_str:", largest_str)
What about max()
highest = max(1, 2, 3) # or max([1, 2, 3]) for lists
This approach is without using
max()function
a = [1,2,3,4,6,7,99,88,999]
max_num = 0
for i in a:
if i > max_num:
max_num = i
print(max_num)
Also if you want to find the index of the resulting max,
print(a.index(max_num))
Direct approach by using function max()
max() function returns the item with the highest value, or the item with the highest value in an iterable
Example: when you have to find max on integers/numbers
a = (1, 5, 3, 9)
print(max(a))
>> 9
Example: when you have string
x = max("Mike", "John", "Vicky")
print(x)
>> Vicky
It basically returns the name with the highest value, ordered alphabetically.
It is easiest to just apply logic to the entire list. You can get the max value of a list using max(list), and you can use the same index function you use above to get the index of this number in the list.
max_val = max(list_c)
idx_max = list_c.index(max_val)
If you want to loop over the values to find the max, then take note of the following:
list.index(value) will find the index of that value in the list. In your case you are writing list.index(index_number) which doesnt make sense. 'i' already represents the list index, so just set idx_max = 'i'.
Further, you should note that if you set max_val to zero, if all the list items are less than zero, it will stay at zero during the loop and never find the max value. It is better to start by setting max_val to the first item of the list ie list_c[0].
list_c = [-14, 7, -9, 2] max_val = list_c[0] idx_max = 0 for i in range(len(list_c)): if list_c[i] > max_val: max_val = list_c[i] idx_max = i
NB 'return' is used to return values from a function. If you want to print the values, use print(list_c, max_val, idx_max).
Looping twice through an array is inefficient. Use built-in enumerate() and max() with the comparison lambda expression returning the second element of the enumeration:
>>> list_c = [-14, 7, -9, 2]
>>> print(max(enumerate(list_c), key=lambda x: x[1]))
(1, 7)