Index of maximum value of a list
up vote
0
down vote
favorite
I need to implement a function that returns the index of the maximum value of a list. I wrote that but it doesn't work. Could someone tell me why?
def maximum_index(lst):
maximum=0
index=0
for i,value in enumerate(lst):
if value>maximum:
maximum=value
index=i
return index
python
New contributor
add a comment |
up vote
0
down vote
favorite
I need to implement a function that returns the index of the maximum value of a list. I wrote that but it doesn't work. Could someone tell me why?
def maximum_index(lst):
maximum=0
index=0
for i,value in enumerate(lst):
if value>maximum:
maximum=value
index=i
return index
python
New contributor
It also should return None when the list is empty but I'm not sure how to do it
– Sophie Gérard
Nov 17 at 14:37
start withif not lst: return None
– Chris_Rands
Nov 17 at 14:39
2
your current method fails if the maximum value is <0
– Chris_Rands
Nov 17 at 14:39
2
As it was already mentionned on your previous question on SO, 'it doesn't work' is not an accurate enough description of the problem your encountered. If you get an error, please include the complete error traceback. If you don't get the expected result for some data, include the relevant data that exhibits the faulty behavior, as well as the output you get vs the expected one.
– Thierry Lathuille
Nov 17 at 14:48
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to implement a function that returns the index of the maximum value of a list. I wrote that but it doesn't work. Could someone tell me why?
def maximum_index(lst):
maximum=0
index=0
for i,value in enumerate(lst):
if value>maximum:
maximum=value
index=i
return index
python
New contributor
I need to implement a function that returns the index of the maximum value of a list. I wrote that but it doesn't work. Could someone tell me why?
def maximum_index(lst):
maximum=0
index=0
for i,value in enumerate(lst):
if value>maximum:
maximum=value
index=i
return index
python
python
New contributor
New contributor
edited Nov 17 at 15:00
Ayxan
1,06715
1,06715
New contributor
asked Nov 17 at 14:36
Sophie Gérard
13
13
New contributor
New contributor
It also should return None when the list is empty but I'm not sure how to do it
– Sophie Gérard
Nov 17 at 14:37
start withif not lst: return None
– Chris_Rands
Nov 17 at 14:39
2
your current method fails if the maximum value is <0
– Chris_Rands
Nov 17 at 14:39
2
As it was already mentionned on your previous question on SO, 'it doesn't work' is not an accurate enough description of the problem your encountered. If you get an error, please include the complete error traceback. If you don't get the expected result for some data, include the relevant data that exhibits the faulty behavior, as well as the output you get vs the expected one.
– Thierry Lathuille
Nov 17 at 14:48
add a comment |
It also should return None when the list is empty but I'm not sure how to do it
– Sophie Gérard
Nov 17 at 14:37
start withif not lst: return None
– Chris_Rands
Nov 17 at 14:39
2
your current method fails if the maximum value is <0
– Chris_Rands
Nov 17 at 14:39
2
As it was already mentionned on your previous question on SO, 'it doesn't work' is not an accurate enough description of the problem your encountered. If you get an error, please include the complete error traceback. If you don't get the expected result for some data, include the relevant data that exhibits the faulty behavior, as well as the output you get vs the expected one.
– Thierry Lathuille
Nov 17 at 14:48
It also should return None when the list is empty but I'm not sure how to do it
– Sophie Gérard
Nov 17 at 14:37
It also should return None when the list is empty but I'm not sure how to do it
– Sophie Gérard
Nov 17 at 14:37
start with
if not lst: return None
– Chris_Rands
Nov 17 at 14:39
start with
if not lst: return None
– Chris_Rands
Nov 17 at 14:39
2
2
your current method fails if the maximum value is <0
– Chris_Rands
Nov 17 at 14:39
your current method fails if the maximum value is <0
– Chris_Rands
Nov 17 at 14:39
2
2
As it was already mentionned on your previous question on SO, 'it doesn't work' is not an accurate enough description of the problem your encountered. If you get an error, please include the complete error traceback. If you don't get the expected result for some data, include the relevant data that exhibits the faulty behavior, as well as the output you get vs the expected one.
– Thierry Lathuille
Nov 17 at 14:48
As it was already mentionned on your previous question on SO, 'it doesn't work' is not an accurate enough description of the problem your encountered. If you get an error, please include the complete error traceback. If you don't get the expected result for some data, include the relevant data that exhibits the faulty behavior, as well as the output you get vs the expected one.
– Thierry Lathuille
Nov 17 at 14:48
add a comment |
4 Answers
4
active
oldest
votes
up vote
0
down vote
accepted
Your code seemed fine to me. I think the only problem is your indentation and that you only check positive values. The way it is indented everything is outside of the function.
Also I added Chris_Rands suggestion of starting with a check, whether the list is empty. Here you go:
def maximum_index(lst):
if not lst:
return None
first = True
maximum=0
index=0
for i,value in enumerate(lst):
if value > maximum or first:
first = False
maximum = value
index = i
return index
This still fails if the maximum is < 0
– Thierry Lathuille
Nov 17 at 14:44
Yeah, I oversaw that as well. Made an edit to fix this.
– FChris
Nov 17 at 14:47
add a comment |
up vote
0
down vote
a better way is to just use max directly:
max(enumerate(lst), key=lambda pair: pair[1], default=(None, None))[0]
enumerate
yields (idx, value)
tuples for the list, the key
checks for the max based on only the value
, and then [0]
grabs the idx
from the tuple from enumerate
add a comment |
up vote
0
down vote
Use max built-in function to find the maximum value and index method to find its index:
def maximum_index(lst):
if (not lst):
return None
return lst.index(max(lst))
Test it:
print(maximum_index([1, 2, 3, 9, 8, 7, 6]))
print(maximum_index())
Output:
3
None
add a comment |
up vote
0
down vote
If you aren't allowed to use max
and index
as suggested by @Ayxan, you should start the loop assuming that the first element is the max, and then updating accordingly.
def maximum_index(lst):
if not lst: # check if list is empty
return None
# initialize maximum and index
index = None
maximum = lst[0]
# loop and update
for i, value in enumerate(lst):
if value > maximum:
maximum = value
index = i
# return index
return index
alist = [5, 6, 1, 199, -1, 0, 12]
print(maximum_index(alist))
print(maximum_index())
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Your code seemed fine to me. I think the only problem is your indentation and that you only check positive values. The way it is indented everything is outside of the function.
Also I added Chris_Rands suggestion of starting with a check, whether the list is empty. Here you go:
def maximum_index(lst):
if not lst:
return None
first = True
maximum=0
index=0
for i,value in enumerate(lst):
if value > maximum or first:
first = False
maximum = value
index = i
return index
This still fails if the maximum is < 0
– Thierry Lathuille
Nov 17 at 14:44
Yeah, I oversaw that as well. Made an edit to fix this.
– FChris
Nov 17 at 14:47
add a comment |
up vote
0
down vote
accepted
Your code seemed fine to me. I think the only problem is your indentation and that you only check positive values. The way it is indented everything is outside of the function.
Also I added Chris_Rands suggestion of starting with a check, whether the list is empty. Here you go:
def maximum_index(lst):
if not lst:
return None
first = True
maximum=0
index=0
for i,value in enumerate(lst):
if value > maximum or first:
first = False
maximum = value
index = i
return index
This still fails if the maximum is < 0
– Thierry Lathuille
Nov 17 at 14:44
Yeah, I oversaw that as well. Made an edit to fix this.
– FChris
Nov 17 at 14:47
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Your code seemed fine to me. I think the only problem is your indentation and that you only check positive values. The way it is indented everything is outside of the function.
Also I added Chris_Rands suggestion of starting with a check, whether the list is empty. Here you go:
def maximum_index(lst):
if not lst:
return None
first = True
maximum=0
index=0
for i,value in enumerate(lst):
if value > maximum or first:
first = False
maximum = value
index = i
return index
Your code seemed fine to me. I think the only problem is your indentation and that you only check positive values. The way it is indented everything is outside of the function.
Also I added Chris_Rands suggestion of starting with a check, whether the list is empty. Here you go:
def maximum_index(lst):
if not lst:
return None
first = True
maximum=0
index=0
for i,value in enumerate(lst):
if value > maximum or first:
first = False
maximum = value
index = i
return index
edited Nov 17 at 14:47
answered Nov 17 at 14:43
FChris
928
928
This still fails if the maximum is < 0
– Thierry Lathuille
Nov 17 at 14:44
Yeah, I oversaw that as well. Made an edit to fix this.
– FChris
Nov 17 at 14:47
add a comment |
This still fails if the maximum is < 0
– Thierry Lathuille
Nov 17 at 14:44
Yeah, I oversaw that as well. Made an edit to fix this.
– FChris
Nov 17 at 14:47
This still fails if the maximum is < 0
– Thierry Lathuille
Nov 17 at 14:44
This still fails if the maximum is < 0
– Thierry Lathuille
Nov 17 at 14:44
Yeah, I oversaw that as well. Made an edit to fix this.
– FChris
Nov 17 at 14:47
Yeah, I oversaw that as well. Made an edit to fix this.
– FChris
Nov 17 at 14:47
add a comment |
up vote
0
down vote
a better way is to just use max directly:
max(enumerate(lst), key=lambda pair: pair[1], default=(None, None))[0]
enumerate
yields (idx, value)
tuples for the list, the key
checks for the max based on only the value
, and then [0]
grabs the idx
from the tuple from enumerate
add a comment |
up vote
0
down vote
a better way is to just use max directly:
max(enumerate(lst), key=lambda pair: pair[1], default=(None, None))[0]
enumerate
yields (idx, value)
tuples for the list, the key
checks for the max based on only the value
, and then [0]
grabs the idx
from the tuple from enumerate
add a comment |
up vote
0
down vote
up vote
0
down vote
a better way is to just use max directly:
max(enumerate(lst), key=lambda pair: pair[1], default=(None, None))[0]
enumerate
yields (idx, value)
tuples for the list, the key
checks for the max based on only the value
, and then [0]
grabs the idx
from the tuple from enumerate
a better way is to just use max directly:
max(enumerate(lst), key=lambda pair: pair[1], default=(None, None))[0]
enumerate
yields (idx, value)
tuples for the list, the key
checks for the max based on only the value
, and then [0]
grabs the idx
from the tuple from enumerate
answered Nov 17 at 14:49
acushner
5,3671826
5,3671826
add a comment |
add a comment |
up vote
0
down vote
Use max built-in function to find the maximum value and index method to find its index:
def maximum_index(lst):
if (not lst):
return None
return lst.index(max(lst))
Test it:
print(maximum_index([1, 2, 3, 9, 8, 7, 6]))
print(maximum_index())
Output:
3
None
add a comment |
up vote
0
down vote
Use max built-in function to find the maximum value and index method to find its index:
def maximum_index(lst):
if (not lst):
return None
return lst.index(max(lst))
Test it:
print(maximum_index([1, 2, 3, 9, 8, 7, 6]))
print(maximum_index())
Output:
3
None
add a comment |
up vote
0
down vote
up vote
0
down vote
Use max built-in function to find the maximum value and index method to find its index:
def maximum_index(lst):
if (not lst):
return None
return lst.index(max(lst))
Test it:
print(maximum_index([1, 2, 3, 9, 8, 7, 6]))
print(maximum_index())
Output:
3
None
Use max built-in function to find the maximum value and index method to find its index:
def maximum_index(lst):
if (not lst):
return None
return lst.index(max(lst))
Test it:
print(maximum_index([1, 2, 3, 9, 8, 7, 6]))
print(maximum_index())
Output:
3
None
edited Nov 17 at 14:51
answered Nov 17 at 14:44
Ayxan
1,06715
1,06715
add a comment |
add a comment |
up vote
0
down vote
If you aren't allowed to use max
and index
as suggested by @Ayxan, you should start the loop assuming that the first element is the max, and then updating accordingly.
def maximum_index(lst):
if not lst: # check if list is empty
return None
# initialize maximum and index
index = None
maximum = lst[0]
# loop and update
for i, value in enumerate(lst):
if value > maximum:
maximum = value
index = i
# return index
return index
alist = [5, 6, 1, 199, -1, 0, 12]
print(maximum_index(alist))
print(maximum_index())
add a comment |
up vote
0
down vote
If you aren't allowed to use max
and index
as suggested by @Ayxan, you should start the loop assuming that the first element is the max, and then updating accordingly.
def maximum_index(lst):
if not lst: # check if list is empty
return None
# initialize maximum and index
index = None
maximum = lst[0]
# loop and update
for i, value in enumerate(lst):
if value > maximum:
maximum = value
index = i
# return index
return index
alist = [5, 6, 1, 199, -1, 0, 12]
print(maximum_index(alist))
print(maximum_index())
add a comment |
up vote
0
down vote
up vote
0
down vote
If you aren't allowed to use max
and index
as suggested by @Ayxan, you should start the loop assuming that the first element is the max, and then updating accordingly.
def maximum_index(lst):
if not lst: # check if list is empty
return None
# initialize maximum and index
index = None
maximum = lst[0]
# loop and update
for i, value in enumerate(lst):
if value > maximum:
maximum = value
index = i
# return index
return index
alist = [5, 6, 1, 199, -1, 0, 12]
print(maximum_index(alist))
print(maximum_index())
If you aren't allowed to use max
and index
as suggested by @Ayxan, you should start the loop assuming that the first element is the max, and then updating accordingly.
def maximum_index(lst):
if not lst: # check if list is empty
return None
# initialize maximum and index
index = None
maximum = lst[0]
# loop and update
for i, value in enumerate(lst):
if value > maximum:
maximum = value
index = i
# return index
return index
alist = [5, 6, 1, 199, -1, 0, 12]
print(maximum_index(alist))
print(maximum_index())
edited Nov 17 at 15:55
answered Nov 17 at 14:52
sal
1,4841412
1,4841412
add a comment |
add a comment |
Sophie Gérard is a new contributor. Be nice, and check out our Code of Conduct.
Sophie Gérard is a new contributor. Be nice, and check out our Code of Conduct.
Sophie Gérard is a new contributor. Be nice, and check out our Code of Conduct.
Sophie Gérard is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352216%2findex-of-maximum-value-of-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It also should return None when the list is empty but I'm not sure how to do it
– Sophie Gérard
Nov 17 at 14:37
start with
if not lst: return None
– Chris_Rands
Nov 17 at 14:39
2
your current method fails if the maximum value is <0
– Chris_Rands
Nov 17 at 14:39
2
As it was already mentionned on your previous question on SO, 'it doesn't work' is not an accurate enough description of the problem your encountered. If you get an error, please include the complete error traceback. If you don't get the expected result for some data, include the relevant data that exhibits the faulty behavior, as well as the output you get vs the expected one.
– Thierry Lathuille
Nov 17 at 14:48