+ 1
explain please
arr = [1, 2, 3, 4, 5, 6] print (arr [2:5][1:3]) why result is [4, 5] and is this index or value?
5 Respuestas
+ 4
That is a list of value that was sliced.
Ok, so first
arr[2:5] = [3, 4, 5]
Let's assign this arr[2:5] to a different variable so its easy to understand
sliced_arr = [3, 4, 5]
sliced_arr[1:3] = [4, 5] #We slice the list from index 1 to 3 so we get the 2nd element to 3rd element
Hence we go the answer [4, 5]
+ 2
you know where i read about this? (is string method?)
+ 2
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2453/
This lesson is about List Slices but it seems like you are not yet in this lesson, but don't worry, if you're curious you can just search in YouTube and Google.
+ 2
I am not 100% sure about it.Yet, I am answering it from my thinking.
if I think,
arr[x:y] then it means ,it will start printing from arr[x] and with end with arr[y-1].
In your question,
here arr[2:5] means start printing from arr[2] which is 3 and end with arr[5-1(4)] which is 5
so it will print [3,4,5] .
Now here comes the second part .
arr[2:5][1:3]
now think arr2 =arr[2:5]=[3,4,5]
and you are printing arr2[1:3]
here arr2[1:3] means print from arr2[1] which is 4 and end with arr2[3-1] which is 5
so it will print [4,5] .
So in total if you print arr[2:5][1:3] it's equivalent to arr2[1:3] =[4,5] .
Hope you understand.
+ 1
thanks guys👍