hi guys, I'm new to python and i can't seem to understand this one line of code right here:

distance, coordinates = min([(someFunction(state, coordinates), coordinates) for coordinates in coordinatesList])

coordinatesList contains a list of paired coordinates (x,y). someFunction returns an int. My question is why isn't distance and coordinates contain the same integer? i mean after the min() function, it should return an int, right? however, my print statements have coordinates as (someX, someY) and distance as some integer. How is this possible?

Min function does not return an int.
Min function gets a sequence and returns the lowest element of that sequence.
In that case this sequence is a list of (distance, coordinates) pairs.

If you have a list of distance and coordinates like:
[(12,(1,2)),
(1,(12,25)),
(0,(1525,13))]
It will return the (0,(1525,13)) element, because the first value of this element (zero) is the lowest among the other elements.

So we have a statement:
distance, coordinates = (0,(1525,13))

Distance will be 0, and coordinates will be (1525,13). It is called sequence unpacking.

Look up the docs:
http://docs.python.org/2/library/functions.html#min
http://docs.python.org/2/tutorial/datastructures.html#comparing-sequences-and-other-types
http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences

commented: Shows knowledge! +14

Thank you so much! you explained perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.