-2

What is the default range in slicing: For example:

ls = [1, 2, 3, 4, 5]
ls[-3::1]  #returns copy of the elements at indexes -3, -2, -1
ls[-3::-1] #returns copy of the elements at indexes -3 till the start of the list i.e ls[-5]
ls[::-1]   #returns copy of the reverse version of the list
ls[::1]    #return the list as is

What is the idea behind that? How does python determine unstated start and end indexes?

CS_EE
  • 399
  • 2
  • 10
  • 2
    If you have a positive step, then the default start is the start and the default end is the end. If you have a negative step then the default start is the end and the default end is the start. The default step is 1. – khelwood Jul 18 '20 at 14:09
  • It's great that you decided to ask community, but this information you can easily find in official docs. So, what's the point? – Olvin Roght Jul 18 '20 at 14:13
  • https://stackoverflow.com/questions/509211/understanding-slice-notation – prax Jul 18 '20 at 14:13

2 Answers2

1

If you have a positive step, then the default start is the start and the default end is the end.

If you have a negative step then the default start is the end and the default end is the start.

The default step is 1. The step cannot be zero.

I think that covers all the possibilities.

khelwood
  • 55,782
  • 14
  • 81
  • 108
0

For example [a:b:c], so a is start index, b is end index and c means step between indices

Krystian
  • 28
  • 4