List slicing in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

List slicing in python

#list slicing Friends=["harry","tom","rohan","sam","divya","45"] Print(friends [0:4])

17th Feb 2022, 5:47 AM
Supriya Swain
6 Answers
+ 3
Yes, you can also give negative values for indices. The negative indexing starts from where the list ends. It means that the index value of -1 gives the last element, and -2 gives the second last element of a list. [-2:-4 ] here, slicing starts from second last element and ends with 4th element from the backwards. It returns [] because, there is no element between these two with the step of 1. You can try this and see the output. [-2:-4:-1] it goes backwards since step value is -1. We already discussed about list slicing a few months ago. Hope it helps you https://www.sololearn.com/discuss/2955122/?ref=app
17th Feb 2022, 2:54 PM
Simba
Simba - avatar
+ 2
Yeah, it returns the elements from 0 to 4 without including 4th element of the friends list. You have any questions than this?
17th Feb 2022, 7:00 AM
Simba
Simba - avatar
0
???
17th Feb 2022, 5:48 AM
Supriya Swain
0
Yes i am in doubt...
17th Feb 2022, 9:41 AM
Supriya Swain
0
Actually I can't understand #list slicing Friends=["harry","tom","rohan","sam","divya","45"] Print(friends [-2 : -4]) Can I write in this way Or the output what's comes
17th Feb 2022, 9:45 AM
Supriya Swain
0
???
17th Feb 2022, 2:09 PM
Supriya Swain