Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Tuples Introduction To Tuples

modifying a list in a tuple, odd behavior

Kenneth was talking about modifying lists inside tuples so I was playing around with that. I got an error (which didn't really surpise me) but it did what I asked anyway (which really confuses me)

>>> tup_list
(1, 2, [1, 2, 3, 4, 5, 6])
>>> tup_list[2] += 7,8,9
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment # error here
>>> tup_list
(1, 2, [1, 2, 3, 4, 5, 6, 7, 8, 9]) # adds 7,8,9 even though it says it can't
>>>

1 Answer

Steven Parker
Steven Parker
231,275 Points

:point_right: It appears to be a spurious error message.

I did get the same thing when I tried. But, you can get the same result with no error by doing this:

tup_list[2].extend([7, 8, 9])

Thanks Steve.