List Functions
Linking of lists

Self referencing in list

In Python, [...] printed inside a list or dictionary represents a self-reference (also known as a recursive data structure or circular reference).
It is Python’s safety mechanism to prevent infinite recursion.

How Self-Reference Works in Memory

When you execute:

L = [1, 2, 3]
L.append(L)

Python does not create a new copy of L. Instead, it stores a pointer (memory reference) to L inside L itself.

L ───> [ 1 , 2 , 3 , (points back to L) ]
                       │
                       └──────────────────┘

Because index 3 points directly to L, evaluating L[3] gives you L. That means:

  • L[3] is L
  • L[3][3] is L
  • L[3][3][3] is L
    You can chain [3] infinitely deep without getting an IndexError.

Why Python Prints [...]

When you call print(L) or str(L), Python visits every element in the list and converts it to text.

Without protection, the formatting process would look like this:

[1, 2, 3, [1, 2, 3, [1, 2, 3, [1, 2, 3, ...

This would crash your program with a RecursionError (maximum recursion depth exceeded).

To prevent this, Python keeps track of all object memory addresses (id()) currently being printed. If it encounters a memory address it is already in the middle of printing, it stops going deeper and prints [...] instead.

Self-Reference in Dictionaries

The exact same thing happens with dictionaries or any mutable container:

Python

d = {}
d['self'] = d

print(d)  # Output: {'self': {...}}

Here, {...} plays the same role for dictionaries that [...] plays for lists.

Common Pitfalls of Circular References

  1. Comparison Operations Crash:

    Comparing two self-referential lists for equality (L == L2) will trigger an infinite loop and raise a RecursionError.

  2. JSON Serialization Fails:

    If you try to pass a self-referential object to json.dumps(L), Python raises:

    ValueError: Circular reference detected

  3. Garbage Collection Overhead:
    While Python can handle cleaning up isolated cyclic references using its cycle-detecting garbage collector, creating unnecessary circular references can make memory management harder for long-running applications.

Extend Function()

It basically copies all the references of the list into the other list

L=[1,2,3]
L.extend(L)
L[0]=1
print[L]     ##[0,1,2,1,2,3]

The second one is not changed because L[0]=1 only cganges the first box and makes it point somewhere else.
note if we used append function, it would have been a different story. Lists > Self referencing in list

Nested List

To add a list a as a nested element inside a list of lists b, use .append(a).

Most Common Way: .append()

Python

a = [1, 2, 3]
b = [[4, 5], [6, 7]]

b.append(a)
print(b)  # [[4, 5], [6, 7], [1, 2, 3]]

.append(a) puts a itself at the end of b as a single nested list.

Watch Out for .extend() vs .append()

  • b.append(a) — Adds a as one nested list inside b:[[4, 5], [6, 7], [1, 2, 3]]

  • b.extend(a) — Unpacks a and adds each individual element (1, 2, 3) directly into b (not nested):[[4, 5], [6, 7], 1, 2, 3]

Other Useful Variations

  • Insert at a specific index (index 0 for the front):

    Python

    b.insert(0, a)  # [[1, 2, 3], [4, 5], [6, 7]]
    
  • Create a new list without modifying b in place:

    Python

    c = b + [a]  # b stays unchanged; c has [a] appended