a=[1,2,3,4,5] a[1:4]=[1] print (a) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

a=[1,2,3,4,5] a[1:4]=[1] print (a)

How is this code working, what is the meaning of [1:4] https://code.sololearn.com/cIScecIhlzaY/?ref=app https://code.sololearn.com/cIScecIhlzaY/?ref=app

24th Apr 2020, 3:19 AM
Akash Yadav
Akash Yadav - avatar
2 Answers
+ 2
It gets a range and replaces it with new list of items. I changed value to 9 so it’s easy to see/understand. a=[1,2,3,4,5] a[1:4]=[9] print (a) Output: [1, 9, 5] 1 is the starting index of the range, the 4 is the ending index of the range (but does not include the item in position 4. So the values 2,3,and 4 get replace with the new value(s). Hope that helps.
24th Apr 2020, 3:53 AM
Chris Persichetti
Chris Persichetti - avatar