0

I've tried the following code but this is giving me a output that i'm unable to predict. Please help me in scanning this code.

l=[['d','e','f'],['a','b','c'],2,3,4,5]
l[:2][1:2]

Output:

[['a', 'b', 'c']]
DuDa
  • 3,718
  • 4
  • 16
  • 36
  • 2
    Did you look at the output of `l[:2]` first? Do you understand why you get that output? Do you understand what slicing that with `[1:2]` does? – chepner Feb 02 '21 at 15:04
  • 2
    Here Your list is `l=[['d','e','f'],['a','b','c'],2,3,4,5]` slicing the list as `l[:2]` will return you `0th` and `1st` element as : `[['d', 'e', 'f'], ['a', 'b', 'c']]` and the 2nd slicer `[1:2]` will return you the `1st` from 1st sliced output. as `[['a', 'b', 'c']]` – Rushikesh Sabde Feb 02 '21 at 15:09
  • With `l[:2]`, you are selecting the first two elements of the main list, more specifically, the elements 0 (inclusive) to 2 (exclusive); so, you are selecting `[['d','e','f'],['a','b','c']]`. Over this result, you are selecting `[1:2]`, which means the elements 1 (inclusive) to 2 (exclusive), thus only `[['a', 'b', 'c']]`. Please, mind that the slicing of lists, in Python, follows the form `start:stop:step`. – nunohpinheiro Feb 02 '21 at 15:09

0 Answers0