Question on lists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Question on lists

Ok.. I have a list L L=['a', 'b', 'c'] Now we know that slicing a list returns a new list (correct me if I'm wrong) so L[0:3] gives a,b Now since list returns a new list/object how does replacement work?? Say L[0:2]=['d', 'e'] print (L) gives a,d,e... how's that possible??..I know list is mutable but slicing always returns a new list... then how does this work??

2nd Aug 2018, 2:35 PM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
10 Answers
+ 3
So true about one notation doing many things! There's more: a=[0,1,2,3,4] del a[1:3] results in a=[0,3,4] I guess the slice produces a new list only when we are doing assignment (that is, when the slice is on the right side of "="). It's a convenient way to get a (shallow) copy of the list. For all other purposes, it's just a generalization of getting an entry by index.
2nd Aug 2018, 4:44 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 5
I think you have the wrong outputs in mind. L[0:1] only returns "a", the first element. So then L[0:1] = ["d", "e"] returns ["d", "e", "b", "c"]. So here's the code that you imagined, you can experiment with it to get a better understanding https://code.sololearn.com/cNhBeXa6Tl0h/?ref=app
2nd Aug 2018, 2:51 PM
Tim Thuma
Tim Thuma - avatar
+ 4
Basically what happens is the same as what happens when you change only one element. e.g. L[0] = ["d"] replaces the first element, L[0:2] = ["d"] replaces the first and second element and so on. Sorry if you don't understand, but I really don't know how to explain this better.
2nd Aug 2018, 3:03 PM
Tim Thuma
Tim Thuma - avatar
+ 4
Tim Thuma your example is good I got the list slice
11th Aug 2018, 10:12 AM
Yesh Jadav
Yesh Jadav - avatar
+ 3
Well yeah Python can do so many incredible things that sometimes its really hard to understand haha.
2nd Aug 2018, 3:13 PM
Tim Thuma
Tim Thuma - avatar
+ 2
@ Tim Thuma..yeah you're right.. I'll edit it right away & check the snippet as well..
2nd Aug 2018, 2:52 PM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
+ 2
@ Tim Thuma right..I checked the code thnx... but do you know how it works internally?? slicing Returns an object (List) but when changing the list itself..(like in L[0:2]=[d','e']) what exactly happens?? how does it replace??
2nd Aug 2018, 2:56 PM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
+ 2
I see... @ Kishalya Saha..thnx for the reply..
3rd Aug 2018, 4:30 AM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
+ 1
No.. No..I do understand the concept @ Tim Thuma but I was just curious about the internal working.. Likewise Python is too smart and one thing does so many other things as well!!(it's contacting that something which returns a new object can alter the original object..) Anyway thnx for the tip & reply!!
2nd Aug 2018, 3:06 PM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
+ 1
You are welcome, Ultra Yam ZGH! And thank you for my first "best answer" mark! 😊
3rd Aug 2018, 11:25 AM
Kishalaya Saha
Kishalaya Saha - avatar