0

I'm trying to get the index of the smallest number in a list, confused by this result.....

nums = [4,0,100] smallest = min(enumerate(nums)) print("smallest = ", smallest) 

Printout: smallest = (0,4)

Shouldn't it be: smallest = (1,0)

0

1 Answer 1

1

You need to use key=lambda x: x[1]) to say min function to check for the minimum value present in the second index, by default it checks in the first index, in which you are having an index value. So, it results (0,4) which is obvious.

Try this,

>>> min(enumerate(nums), key=lambda x: x[1]) (1, 0) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.