Python Lists Cyclic References by YOKESH

Lists are weird because they took Weed !

Lists are weird because they took Weed !

Some days back, I was just digging deep into python lists and i found some thing really interesting and confusing at the same time. ”I will make you confusing too” . Chalooo, Let’s start !….

Some days back, I was just digging deep into python lists and i found some thing really interesting and confusing at the same time. ”I will make you confusing too” . Chalooo, Let’s start !….

Before going into weird lists, let’s have some basics on ‘=’ operator that works as a alias operator on lists and on more data types. If i say list1 = list2, both lists are pointing to same memory address of the mutable list object. GOT IT ? …. Nooo ? Look Below !

                my_list1 = [1,2,3]
my_list2 = my_list1

my_list2.append(4)

print(my_list1)
print(my_list2)
output : Both prints [1,2,3,4]
            

Why this happens ? Because my_list2 is just an aliased name to my_list1.Changes made to my_list2 will be reflected in my_list1 . So, Be careful while playing with mutable data types in python .

Let’s dig into weird lists behavior,

                weird_list1 = [1,2,3,4,5]

weird_list2 = weird_list1

weird_list2.append(weird_list1)

print(weird_list1)
print(weird_list2)
            

Guess the output of the above code !

The outputs are not [1,2,3,4,5,[1,2,3,4,5]] and

[1,2,3,4,5,[1,2,3,4,5]] …..feeling weird right ? The correct outputs are

[1, 2, 3, 4, 5, […]] and [1, 2, 3, 4, 5, […]]. Let’s see how this output came..

Initially, both lists are pointing to same object list with items 1,2,3,4,5

NOTE : In the above image , I wrote wierd instead of weird (typo mistake)

After Appending , weird_list2 looks like this,

NOTE : In the above image , I wrote wierd instead of weird (typo mistake)

That means, Weird_list1 is storing in the weird_list2 as a 6th element in the index 5. That means, element at index 5 of weird_list2 is storing whole weird_list1 as it’s 6th element. But, both lists weird_list1 and wierd_list2 are aliases to each other. So, element 6 of weird_list2 is literally equal to the weird_list2 due to aliasing .Changes made to element 6 of the weird_list2 will reflect in the whole weird_list2 .Here, Cyclic Reference happening between the element of a list and the list itself. python handles these kind of cyclic references creating elements using Ellipsis represented by

python replaces the element that is creating cyclic reference with the list itself with

So, the output while printing weird_list1 or 2gives [1,2,3,4,5,[…]],meaning that there is a element / item in the list that is creating cyclic reference with the list itself.

                weird_list1 = [1,2,3,4,5]

weird_list2 = weird_list1

weird_list2.append(weird_list1)

print(weird_list1)
print(weird_list2)

print(weird_list1[5])
print(weird_list1[5][5])
print(weird_list1[5][5][5][5])
            

Guess what this prints ,

As we know that weird_list1 and weird_list2 has a 5th element that has a cyclic reference, so 1st ,2nd and 3rd print statements prints [1,2,3,4,5,[…]]. the 4th and 5th print statements also prints the same [1,2,3,4,5,[…]] .

How weird_list1[5][5] gives [1,2,3,4,5,[…]]

weird_list2[5] has cyclic reference to wierd_list2 , and weird_list2[5][5] has cyclic reference to weird_list2[5] ……and so on ..for weird_list2[5][5][5]…..

Even If you print weird_list2[5][5][5] , it gives you [1,2,3,4,5,[…]]

This is the process python handles cyclic references.

Please hit the like and follow button if you think I gave some knowledge on python.

Thank YOU


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies and get more readers

Join other developers and claim your FAUN account now!

Avatar

yokesh Chowdary

Backend developer, Clarivate

@yokee-ss-hh
Loves Backend web development, Consistently playing with Datastructures and algorithms.
Stats
11

Influence

617

Total Hits

3

Posts