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

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

Please explain this to me

3rd Sep 2019, 2:33 AM
Prajwal Sharma
Prajwal Sharma - avatar
4 Answers
+ 1
Actually your arr list is one dimension only and in print statement you are trying to getting input from 2d dimension arr but your arr is one dimension so that's why which gives you empty list. #Output=[ ]
3rd Sep 2019, 2:55 AM
Maninder $ingh
Maninder $ingh - avatar
+ 1
Seb TheS i know list is in the code not a 2d but Prajwal Sharma try to retrieve the numbers from a 2d list which is actually a one dimension so that's why he getting [ ] as output.
6th Sep 2019, 2:20 AM
Maninder $ingh
Maninder $ingh - avatar
0
Let's break print(arr[1:3] [2:5]) in parts: arr = [1, 2, 3, 4, 5, 6] a = arr[1:3] b = a[2:5] print(b) a will get assigned the value arr[1:3], which evaluates to [2, 3], thus a = [2, 3]. Then b will get assigned the value a[2:5], but because [2, 3] has no values between indices 2 and 5, thus a[2:5] will evaluate to [ ] (empty string), and b = [ ]. Then b was printed, and an empty list was displayed.
3rd Sep 2019, 9:24 AM
Seb TheS
Seb TheS - avatar
0
Maninder $ingh That example has nothing to do with 2D arrays.
3rd Sep 2019, 9:27 AM
Seb TheS
Seb TheS - avatar