0

Implementing the recursive definition of Grays Code from Knuth I need to add values to tuples, I have:

In [54]: tuple_one = (1,0,0)

In [55]: tuple_one
Out[55]: (1, 0, 0)

In [56]: tuple_one += (1,)

In [57]: tuple_one
Out[57]: (1, 0, 0, 1)

However if I try and assign the above I get a syntax error

In [63]: B = tuple_one += (1,)
  File "<ipython-input-63-537be8a059fc>", line 1
    B = tuple_one += (1,)
               ^
SyntaxError: invalid syntax

Why is this ? Regards Paul

3
  • 1
    B = tuple_one += (1,) really is invalid syntax. Try B = tuple_one + (1,) Commented Jul 13, 2014 at 16:04
  • A tuple is immutable in Python. If you want to add items, consider a list (that you can then append to). Commented Jul 13, 2014 at 16:07
  • @BartoszKP hence I did not put this as an answer. I am merely suggesting that the OP considers a different data structure, as creating multiple tuple objects is less efficient than modifying a single list object. Commented Jul 13, 2014 at 16:14

3 Answers 3

2

Explanation from Python Docs

An augmented assignment evaluates the target (which, unlike normal assignment statements) is evaluated only once. An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once.

Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.

With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments

So, while assigning to tuples or/and multiple targets in a single statement, do not use augmented assignment statements.

Instead, use

B = tuple_one = tuple_one + (1,)
Sign up to request clarification or add additional context in comments.

2 Comments

Why is x evaluated twice in a x = x + 1 statement -- in other words what's the purpose of the second evaluation?
Because until the expression is evaluated, the python interpreter doesn't know if the variable/object after assignment expression is x or y or something else. i.e it can be x = x + 1 or x = y + 1.
1

That's not a list of tuples. Its just a tuple.

And the reason it doesn't work is because it is actually invalid syntax. It is literally equal to

B = tuple_one = tuple_one + (1,)

if you want to save the value of tuple_one then do it in two line

tuple_one += (1,)
B = tuple_one

if you don't then just use the + operator

B = tuple_one + (1,)

1 Comment

Thanks, I know it's not a list but wanted to get it to work on an element of the list before looking at the whole list. Cheers Paul
0

Your "list of tuples" is not a list of tuples, it is a single one. You can tell that it isn't a list, because there are no square brackets ([]).

+= is used to modify in place, so a+=1 would be used instead of a = a+1. The difference is that a+1 returns the value of a plus 1, but +=1 does not return anything:

>>> a = 1
>>> x = a+=1 #DOES NOT WORK
  File "<stdin>", line 1
    x = a+=1
          ^
SyntaxError: invalid syntax
>>> x = a+1
>>> x
2
>>> 

Here is your edited line:

B = tuple_one + (1,)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.