Sep-17-2018, 10:26 AM
(Sep-17-2018, 10:08 AM)wavic Wrote: If you don't want to modify the original why not tuple?
>>> t = ('one',) >>> t += ('two',) >>> t ('one', 'two') >>>Hm! This actually modifies it
No, it creates a new
t
object Output:In [28]: t = ('one',)
In [29]: id(t)
Out[29]: 140190907161512
In [30]: >>> t += ('two',)
In [31]: id(t)
Out[31]: 140190903830408
With lists, +=
will be equivalent to append
(or extend
)Output:In [34]: id(t)
Out[34]: 140190903120520
In [35]: t = [1]
In [36]: id(t)
Out[36]: 140190903091912
In [37]: t += [2]
In [38]: id(t)
Out[38]: 140190903091912
In [39]: t
Out[39]: [1, 2]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.