0
>>> x = [1,3]
>>> x
[1, 3]
>>> x[0]
1
>>> x[1]
3
>>> x,y = [1,3]
>>> x
1
>>> y
3
>>> x[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable

As I understand it, a list is a value in and of itself. As such, it can be assigned to a variable.

This is evident in the above code, where x = [1,3] and x returns the list value of [1,3]. However, if there are two variables to the left of the assignment operator, things change. The list to the right of the variable is no longer the value, but, instead, the elements of the list are the values.

Would someone be kind enough to explain why this is so. Thanks in advance.

Perhaps I've misunderstood what "unpacking" is. Do the rules change when there are multiple variables to the left of the assignment operator?

efw
  • 449
  • 3
  • 16
  • `x, y = [1, 3]` is an unpacking and `x = [1, 3]` isn't. You can unpack a length-1 list into a single target with something like `[x] = [1]`, but assigning to `x` on its own doesn't trigger unpacking. – user2357112 Oct 11 '17 at 03:13
  • Yes, the rules change when there are multiple variables to the left of the assignment operator. This triggers 'unpacking' of the object on the right of the assignment operator, and a corresponding assignment to the relevant variables on the left. Why is this so? Because someone (probably Guido) decided it was a good feature for Python. – Andrew Guy Oct 11 '17 at 03:27
  • ok, so I'm not understanding the difference between "assigning a value to a variable" and "unpacking". Because there is an assignment operator, my assumption is that the value on the right is being assigned to the variable on the left. In this case, a list is assigned to a single variable on the left, which makes sense to me. However, when there is more than one variable on the left, it doesn't assign the value -- the list -- but the elements of the list. This doesn't make sense to me because the value is the list itself, not the elements of the list. – efw Oct 11 '17 at 03:37
  • @Andew Guy -- ok, I got my comment in after yours, but I think I understand now. Thanks. So with multiple variables to the left of the operator you are triggering "unpacking'. And as I understand it, there have to be the same number of variables on the left of the operator as there are elements in the list. – efw Oct 11 '17 at 03:40

1 Answers1

-1

This is a simple assignment:

x = [1, 3]

This is unpacking:

x, y = [1, 3]

Maybe this is what you want:

x = y = [1, 3]
x[0] # 1

Explanation

Observe that x, y is actually convenience for (x, y):

(x, y) = [1, 3]

x = 1, 3
x # (1, 3)

That's why having multiple values on the left behaves differently.

aaron
  • 39,695
  • 6
  • 46
  • 102
  • thanks. I understand it now. I was just trying to understand the concept and testing it by indexing the list values. – efw Oct 11 '17 at 03:48
  • You're welcome! I added a little explanation on why having multiple values on the left behaves differently. – aaron Oct 11 '17 at 03:58
  • `x, y` is **not** a "convenience" for `(x, y)` – it's the comma that signifies a tuple, not the parentheses (although these are sometimes required to eliminate ambiguity). See: [wiki.python.org/moin/TupleSyntax](https://wiki.python.org/moin/TupleSyntax) – Zero Piraeus Oct 11 '17 at 23:14