1

as we know that tuple is immutable but if a tuple holds a list inside,can we append an respective list?

t=(1,2,3,"hi",[2,3,4],True)
t[4].append(7)
print(t)

output

(1,2,3,"hi",[2,3,4,7],True)
Underoos
  • 4,708
  • 8
  • 42
  • 85
  • 8
    Possible duplicate of [Why can tuples contain mutable items?](https://stackoverflow.com/questions/9755990/why-can-tuples-contain-mutable-items) – CDJB Nov 27 '19 at 13:15
  • 1
    Why do you ask the exact same question twice? [Here](https://stackoverflow.com/questions/59071002/appending-an-item-to-a-list-which-is-the-item-of-tuple) if it was already marked as duplicate? – LeoE Nov 27 '19 at 13:15

1 Answers1

2

Because under the hood python is not holding the array itself, but its reference. So as long as the reference doesn't change it's ok!!

So imagine the memory addresses

0x00 = 1
0x01 = 2
0x02 = 3
0x03 = "hi"
0x04 = 0x10
0x05 = True

0x10 = 2
0x11 = 3
0x12 = 4

If you add more elements to the array, the values between 0x00 and 0x05 won't change, and that is the immutable behavior on python

Felipe Endlich
  • 540
  • 5
  • 24