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
2 Antworten
+ 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.