Explain this line of code.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Explain this line of code..

a = [1,2] a.append(a) print(a) '''then output is [ 1, 2, [. . .]] what the hack is this''' print(a == a[2][2][2]) '''then the output is True how???'''

11th Sep 2018, 4:22 PM
Meet Rajpopat
Meet Rajpopat - avatar
17 Answers
+ 3
Okay, the index of a in a is 2, alright? So this a contains a once more, also in position 2, because it is the same list. (And so on.) So none of the a's has an index 3, they all only have indexes 0, 1 and 2. Let's write it out a few steps. [1, 2 [1, 2, [1,2, [...]]]] You see that at index 2, the next inner a starts (eternally going on). But after index 2 there's nothing. So if you index by [3], you get an index error, no matter how deep you go.
11th Sep 2018, 5:57 PM
HonFu
HonFu - avatar
+ 2
confuse
11th Sep 2018, 4:28 PM
Maninder $ingh
Maninder $ingh - avatar
+ 1
Maninder Singh same here bro😂😂
11th Sep 2018, 4:29 PM
Meet Rajpopat
Meet Rajpopat - avatar
+ 1
By appending a to a, you get something like an eternal list: a in a in a in a... And every time, no matter how deep you look, there's another reference to a in position 2. Try: a == a[2][2] or: a == a[2][2][2][2] That's also all True. :)
11th Sep 2018, 4:39 PM
HonFu
HonFu - avatar
+ 1
HonFu thank you so much I got it, have a nice day man😁
11th Sep 2018, 6:01 PM
Meet Rajpopat
Meet Rajpopat - avatar
+ 1
Thank YOU because you forced me to think about it for the first time (after having it 'solved' in challenges over and over ^^').
11th Sep 2018, 6:02 PM
HonFu
HonFu - avatar
+ 1
Meet Rajpopat ..a.append modifying the list as nested [. .] .... according to index "2' it shows same values same time. ..... finally it checks hence true :) 🙌👈💃nice question
12th Sep 2018, 4:59 PM
Sri Raman Prasad
+ 1
Roberto now that I'm looking at your last question again in dailight, I think I maybe (finally) understood why you expect 'a' only once. You are looking at the content of a instead of a. There's 1 and 2 in the container, and if you put that into a, there should be one container with 1 and 2 in it, and over. Right? But actually you have to think about three objects: 1, 2 and the 'container' a, each having their own place and adress in memory (which you can check using the id function). So when you append a to a, you append the name of the container itself to a! So what you have in a in location 2 is the name of the container as it is right now. You could also change the list afterwards, like with a.insert(1, 'x'). Then the location of a in a would move one to the right, and every a in a in a and so on would contain 'x' at position 1 (because in reality it is the very same object). If you want to add just the content, you could do this: a.append(a[:]) Now the list is a copy of a, a different object.
16th Sep 2018, 2:45 PM
HonFu
HonFu - avatar
+ 1
a = [1,2] list before the append # index i: 0,1, 2 a.append(a) = [1,2[1,2]] #This is what I expect to append: all the container a and its contents to the last element of a, so the container a will occupy index 2 of my new list. I was sure that "appending an element to a list" means adding it to the last indexed position inside the list....but maybe I am wrong... Aside of that, I was trying also to do this : a + (a) gives me [1,2,1,2] instead of expected [1,2[1,2]]....but this maybe off topic. and yes, I tried with id function to chk the address in memory: >>> a [1, 2, [...]] >>> id(a) 62967688 >>> id(a[0]) 497642976 >>> id(a[1]) 497643008 >>> id(a[2]) 62967688 so id(a) == id(a[2]) , but this is not still confirming me the infinite mirroring at index 2 of this new list... I hope my discussion won't raise you a headache :-) anyway appreciate your effort and helping comments!
18th Sep 2018, 8:04 AM
Roberto
+ 1
Roberto, let's take a look at the infinity issue by taking a look at this code: https://code.sololearn.com/c8FJrflBhwp6/?ref=app If you try it you'll see that you can indeed go deeper and deeper and always find the same. On your own computer you can exchange the for-instruction with 'while True:' - then you'll get an eternal loop. A name (like 'x' in 'x = 5') is only a reference. The actual object 5 sits in a spot in memory, and by writing print(x) or something, you tell python: 'Go to the object we refer to as 'x'!' A list is basically a bundle of references, an easy way to have them all in one place. So if you write print(a), you will get a list of the references in a. If you write print(a[2]), Python looks what object the name at index 2 refers to, and confirms: 'Ah, Object 'a' again. So again you get told what's in 'a'. And this happens again in deeper layers, because it is actually the same list 'opened' over and over again.
18th Sep 2018, 10:20 AM
HonFu
HonFu - avatar
+ 1
If you make a second list with the same content, like 'b = [1, 2]' and then write a.append(b), then you don't get that infinity thing, so the result looks like what you expected. But actually the process is the same: You add the NAME 'b' to the list a. So when you access a[2], Python will look what is stored under b. This means, if you change b later, the content of b in b will change too, because it is the very same b.
18th Sep 2018, 10:31 AM
HonFu
HonFu - avatar
0
HonFu yeah I got it but print(a[2][3]) gives indexerror and print(a[2][2]) gives [1,2,[. . . ]] can you explain why because there is in no [2] element in that list
11th Sep 2018, 4:47 PM
Meet Rajpopat
Meet Rajpopat - avatar
0
hello
11th Sep 2018, 10:14 PM
Programist666
0
can't understand this one... first of all why the output of print(a) is [1,2,[...]] instead of [1,2,[1,2]]?
15th Sep 2018, 8:47 PM
Roberto
0
Because the inner list contains another list and another list and another list... If you try to write it out, it will never end. It's like looking into two facing mirrors. So this [...] is an 'abbreviation' of this eternity of 'a in a in a in a ...' Oh, maybe this is confusing you: After we append a, the list is not [1, 2], but it's [1, 2, a]. So if we open the 'box' a, we find 1, 2 - and another box a, and if we open that one... (Does this sonehow make it more clear?)
15th Sep 2018, 9:02 PM
HonFu
HonFu - avatar
0
thanks HonFu , maybe I'm near to understand... however I'm missing something: to my 'primitive sense of logic' , by appending 'a' to the original 'a' I can see it at index 2 only once instead of being nested several times (like in the mirrors)...I wish I could understand why the 'a' at index 2 of the new list is eternally going on...
15th Sep 2018, 9:13 PM
Roberto
0
Okay, another angle. Let's say, we got two lists: a before the append, and a after the append. The first one is [1, 2]. The second one is [1, 2, a]. Right? Cause we just added a. So imagine you are a computer. Someone tells you: Look at a[2]. What's there? 'a' is the answer. So now you ask: What is at position 2 of a in a? (a[2][2]). So Python looks at index 2 of a, as asked, 'enters' a, (which is the very same list - yeah, makes you dizzy!) and looks at what's at the spot 2 of that list. Surprise: a again. So now you ask: What's at a[2][2][2]? Python looks at a[2], finds and opens a, looks at 2, finds a all over again, opens it ...
15th Sep 2018, 9:22 PM
HonFu
HonFu - avatar