I think the accepted answer is great, but why don't you do it explicitly? I feel more people would understand your code, and that is in agreement with PEP 8:
max_value = max(my_list)
max_index = my_list.index(max_value)
This method is also about three times faster than the accepted answer:
import random
from datetime import datetime
import operator
def explicit(l):
max_val = max(l)
max_idx = l.index(max_val)
return max_idx, max_val
def implicit(l):
max_idx, max_val = max(enumerate(l), key=operator.itemgetter(1))
return max_idx, max_val
if __name__ == "__main__":
from timeit import Timer
t = Timer("explicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(100)]")
print "Explicit: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
t = Timer("implicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(100)]")
print "Implicit: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
Results as they run in my computer:
Explicit: 8.07 usec/pass
Implicit: 22.86 usec/pass
Other set:
Explicit: 6.80 usec/pass
Implicit: 19.01 usec/pass
Answer from Escualo on Stack Overflowpython - Pythonic way to find maximum value and its index in a list? - Stack Overflow
finding max in list
for loop - How to find the index of the max value in a list for Python? - Stack Overflow
how to find maximum from a list using a for loop in pythonic way?
Videos
I think the accepted answer is great, but why don't you do it explicitly? I feel more people would understand your code, and that is in agreement with PEP 8:
max_value = max(my_list)
max_index = my_list.index(max_value)
This method is also about three times faster than the accepted answer:
import random
from datetime import datetime
import operator
def explicit(l):
max_val = max(l)
max_idx = l.index(max_val)
return max_idx, max_val
def implicit(l):
max_idx, max_val = max(enumerate(l), key=operator.itemgetter(1))
return max_idx, max_val
if __name__ == "__main__":
from timeit import Timer
t = Timer("explicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(100)]")
print "Explicit: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
t = Timer("implicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(100)]")
print "Implicit: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
Results as they run in my computer:
Explicit: 8.07 usec/pass
Implicit: 22.86 usec/pass
Other set:
Explicit: 6.80 usec/pass
Implicit: 19.01 usec/pass
There are many options, for example:
import operator
index, value = max(enumerate(my_list), key=operator.itemgetter(1))
#I have 2 lists. First, i need to find the max value in list one. the value i want #from list 2 should be the one found in the same index as the max in list one. #example: since 135 is larger, i want to return 15 #because 135 is index 1, and 15 is index 1. LIST_1 = [101,135] LIST_2=[20,15] #i would really appreciate any help, any hints! :D
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)
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?