+ 1

The Output is [4,5], but why? 🤔

arr=[1,2,3,4,5,6] print(arr[2:5][1:3]) Output [4,5]

28th Mar 2018, 4:41 AM
Yohann
3 Respuestas
+ 7
Once typo corrected to: print(arr[2:5][1:3]) You need to understand Python array slicing: https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/ array[start index:excluded end index] arr[2:5] is tmp = [3,4,5] and then, tmp[1:3] = [4,5]
28th Mar 2018, 5:55 AM
visph
visph - avatar
+ 10
Yohann isn't it "print(arr[2:5][1:3])" instead of "print(arr[2:5][1,3])"?
28th Mar 2018, 5:00 AM
Burey
Burey - avatar
+ 1
Traceback (most recent call last): File "..\Playground\", line 2, in <module> print(arr[2:5][1,3]) TypeError: list indices must be integers or slices, not tuple you are calling a tuple in another part of the archive, not the array that you have in the code that you wrote, right?
28th Mar 2018, 5:15 AM
Luis Beyghau