pythonlistappendtuplesiterable-unpacking

Python, is this a bug, append to a list within a tuple results in None?


This is one of the shortest examples I've written in a long time

I create and update a tuple3

In [65]: arf=(0,1,[1,2,3])

In [66]: arf=(arf[0],arf[1], arf[2] )

In [67]: arf
Out[67]: (0, 1, [1, 2, 3])

So the reassignment worked.

Now I try to change it's contents.

In [69]: arf=(arf[0],arf[1], [2] )

In [70]: arf
Out[70]: (0, 1, [2])

In [71]: arf=(arf[0],arf[1], arf[2].append(3) )

In [72]: arf
Out[72]: (0, 1, None)

I get back None??? Hey, what gives? Sorry I'm a python noob.


Solution

  • list.append() always returns None

    so arf[2].append(3) will append 3 to arf and return None

    you never get to see this change to arf[2] because you are immediately rebinding arf to the newly created tuple

    Perhaps this is what you want

    arf = (arf[0], arf[1], arf[2]+[3])