Explanation:
Create an empty list
x = []
x is an empty list
Memory-wise, x is a mutable object
Create a single-element tuple
t = (x,)
t is a tuple containing one element
That element is the same list x
Important: the comma , makes it a tuple
Structure so far:
t → ( x )
x → []
Append the tuple to the list
x.append(t)
You append t inside the list x
Now x contains t
Since t already contains x, this creates a circular reference
Circular structure formed:
t → ( x )
x → [ t ]
Visually:
t
└── x
└── t
└── x
└── ...
Access deeply nested elements
print(len(t[0][0][0]))
Let’s break it down step by step:
๐น t[0]
First element of tuple t
This is x
๐น t[0][0]
First element of list x
This is t
๐น t[0][0][0]
First element of tuple t
This is again x
So:
t[0][0][0] == x
Final Step: len()
len(x)
x contains exactly one element
That element is t
Therefore:
print(len(t[0][0][0])) # → 1
Final Output
1


0 Comments:
Post a Comment