(Aug-22-2019, 08:33 AM)Malt Wrote: I'm not understanding this part.. how this min works here? say for iteration 1, it will do like min([110,201,305],key=10).. is my understanding right. Please clarify me and why we need key here? I mess up 
Also, how 201 is min when 110 is already there? I feel I'm over looking
It's always good to start with built-in help:
>>> help(min)
Help on built-in function min in module builtins:
min(...)
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
(END)
Not the easiest read, but one can observe
key=func
In simpler terms it can be described as:
Quote:For complex processing, min() can be called with an optional "key=" function that transforms each element before comparison. The key function takes in 1 value and returns 1 value, and the returned "proxy" value is used for finding minimum value.
What happens under the hood? Exactly this: every item in iterable is processed by key function and returned value is used to find minimum value.
It can be illustrated by this example:
>>> nums = [110, 201, 305]
>>> [num % 100 for num in num] # find reminders
[10, 1, 5]
>>> min(num % 100 for num in nums) # find smallest reminder
1
Python does it more efficiently but as general idea one can describe principle this way:
>>> nums = [110, 201, 305]
>>> reminders = [num % 100 for num in nums]
>>> i = reminders.index(min(reminders)) # find index of item with minimum value in reminders
>>> nums[i] # find corresponding value in nums
201