Tuple unpacking order changes values assigned

tuple matching python
variable annotation cannot be combined with tuple unpacking
python tuple
python unpack list assignment
python tuple unpacking for loop
tuple reduplication
get first value of tuple python
numpy unpack tuple

I think the two are identical.

nums = [1, 2, 0]    
nums[nums[0]], nums[0] = nums[0], nums[nums[0]]    
print nums  # [2, 1, 0]

nums = [1, 2, 0]    
nums[0], nums[nums[0]] = nums[nums[0]], nums[0]    
print nums  # [2, 2, 1] 

But the results are different. Why are the results different? (why is the second one that result?)

Python Tuples, elements are put between parentheses instead of square bracket. Python uses a special syntax to pass optional arguments (*args) for tuple unpacking. This means that there can be many number of arguments in place of (*args) in python. All values will be assigned to every variable on left hand side and all remaining values will be assigned to *args .For better understanding consider the following code. Example 2

You can define a class to track the process:

class MyList(list):
    def __getitem__(self, key):
        print('get ' + str(key))
        return super(MyList, self).__getitem__(key)
    def __setitem__(self, key, value):
        print('set ' + str(key) + ', ' + str(value))
        return super(MyList, self).__setitem__(key, value)

For the first method:

nums = MyList([1, 2, 0])
nums[nums[0]], nums[0] = nums[0], nums[nums[0]]

the output is:

get 0
get 0
get 1
get 0
set 1, 1
set 0, 2

While the second method:

nums = MyList([1, 2, 0])
nums[0], nums[nums[0]] = nums[nums[0]], nums[0]

the output is:

get 0
get 1
get 0
set 0, 2
get 0
set 2, 1

In both methods, the first three lines are related to tuple generation while the last three lines are related to assignments. Right hand side tuple of the first method is (1, 2) and the second method is (2, 1).

In the assignment stage, first method get nums[0] which is 1, and set nums[1] = 1, then nums[0] = 2, second method assign nums[0] = 2, then get nums[0] which is 2, and finally set nums[2] = 1.

Unpacking a Tuple in Python, But the results are different. Why are the results different? (why is the second one that result?) Prerequisites - 2 important Points. Lists are mutable. The main part  Python gives the syntax to pass optional arguments (*arguments) for tuple unpacking of arbitrary length. All values will be assigned to every variable in the order of their specification and all remaining values will be assigned to *arguments . Let’s consider the following code. Example. Live Demo

It's because of that python assignment priority is left to right.So in following code:

 nums = [1, 2, 0]
 nums[nums[0]], nums[0] = nums[0], nums[nums[0]]

It first assigned the nums[0] to nums[nums[0]] means nums[1]==1 and then since lists are mutable objects the nums would be :

[1,1,0]

and then nums[nums[0]] will be assigned to nums[0] which means nums[0]==2 and :

nums = [2,1,0]

And like so for second part.

Note that the important point here is that list objects are mutable and when you change it in a segment of code it can be change in-place. thus it will affect of the rest of the code.

Evaluation order

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

Unpacking in Python: Beyond Parallel Assignment, Unpacking a Tuple in Python, When we put tuples on both sides of an assignment operator, a tuple unpacking operation takes place. The values on the right are assigned to the variables on the left according to their relative position in each tuple . In this lesson, you’ll learn about tuple assignment, packing, and unpacking. A literal tuple containing several items can be assigned to a single object. Assigning the packed object to a new tuple unpacks the individual items into the objects in the new tuple: >>>

In the first example, what happens is nums[1] gets set to 1, and then nums[0] gets set to 2, as you might expect.

In the second example, nums[0] gets set to 2, and then nums[2] gets set to 1. This is because, in this case, the left hand side nums[nums[0]] is really referencing nums[2] when the assignment happens, because nums[0] had just been set to 2.

Python List list() Method, When we are unpacking values into variables using tuple unpacking, the sets in unpacking operations unless the order of assignment isn't important to our code. In this case, since we're using unpacking in Python, no changes are needed  So, we see that ‘a’ and ‘c’ are assigned the first and last value whereas, *b is assigned with all the in-between values. Unpacking can also be done with the help of a function. A tuple can be passed in a function and unpacked as a normal variable. This is made simpler to understand by the following code,

Tuple unpacking order changes values assigned - Article, But if we try to use item assignment to modify one of the elements of the tuple, we get an Once Python has created a tuple in memory, it cannot be changed. Basics of unpacking a tuple and a list If you write variables on the left side separated by commas,, elements of a tuple and a list on the right side will be assigned to each variable. The same applies to tuples and lists (the following examples are described by tuples).

Tuple unpacking order changes values assigned, f) The variable one_value is assigned a string value 'hello' followed by a trailing comma(,). Thus it is So the output is: 2 i) The sequence unpacking takes place here. But the tuples are immutable i.e. we cannot change the values assigned​  One use case is if you are storing a large number of small sequences where the values rarely change but on few occasions they might want to. For a small but non-zero length sequence, the memory consumption of tuple (60-bytes for one-element) vs list (104 bytes) and make a difference.

9. Tuples, After the call returns, we can use tuple assignment to unpack the parts of the returned Here, X and L change after the call, but only because the code said so​. Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called. But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Comments
  • It's all about Evalutation Order and Mutability of lists...Nice Question @henry
  • This question tripped me out. I honestly thought the statements were the same too. This is a great example of how you need to know the order of operations when evaluating a statement and lists being mutable. Thanks for asking the question!
  • Lines like this would have worked so much nicer if the design had been for a[b], c[d] = e, f to evaluate a, b, c, d, e, and f in that order and then assign, but it's too late to change things now.
  • @user2357112 yeah, I guess they're not making these changes because they don't want to break the present things. We need to wait till python4 where they release another backwards incompatible product.