Today I came across this expression:
(x,_),(y,_) = load_data()
...and I'm wondering what is the order of assignment.
For example x,x,x = 1,2,3
set x
to 3
from my test, does it actually set x
to 1, 2, than 3?
What's the rule it follows? And what happens in more complex conditions like the first code snippet?
While list comprehensions are still the shiniest tool, destructuring is where the power lies. Destructuring assignments are a shorthand syntax for mapping one or more variables from the left-hand side to the right-hand side counterparts. It’s handy in unpacking values as we shall see shortly.
my_list = [67, 2, 999, 1, 15] # this prints the unordered list print ("Unordered list: ", my_list) # sorts the list in place my_list.sort () # this prints the ordered list print ("Ordered list: ", my_list) If the list is already sorted then it will return None in the console.
While list comprehensions are still the shiniest tool, destructuring is where the power lies. Destructuring assignments are a shorthand syntax for mapping one or more variables from the left-hand side to the right-hand side counterparts.
myobj = types.SimpleNamespace (x=1, y=2) mydict = {"a": 1, "b": 2} with kwargs_as_modules (one=myobj, two=mydict): from one import a, b from two import x, y assert a == x, b == y You can destruct a python dictionary and extract properties by unpacking with .values () method:
The relevant part of the documentation on assignment statements is:
If the target list is a comma-separated list of targets, or a single target in square brackets: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.
(Emphasis mine: that's how the order is determined.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With