The documentation implies this has a few useful properties:
word[:2] # The first two characters
word[2:] # Everything except the first two characters
Here’s a useful invariant of slice operations:
s[:i] + s[i:]equalss.For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of
word[1:3]is2.
I think we can assume that the range functions act the same for consistency.
Answer from Toomai on Stack OverflowThe documentation implies this has a few useful properties:
word[:2] # The first two characters
word[2:] # Everything except the first two characters
Here’s a useful invariant of slice operations:
s[:i] + s[i:]equalss.For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of
word[1:3]is2.
I think we can assume that the range functions act the same for consistency.
Here's the opinion of Guido van Rossum:
[...] I was swayed by the elegance of half-open intervals. Especially the invariant that when two slices are adjacent, the first slice's end index is the second slice's start index is just too beautiful to ignore. For example, suppose you split a string into three parts at indices i and j -- the parts would be a[:i], a[i:j], and a[j:].
[Google+ is closed, so link doesn't work anymore. Here's an archive link.]
Python beginner here, coming from a Matlab and R background... so there's been a bit of a hurdle for me when it comes to understanding indexing in Python.
My current brain fuck has to do with wrapping my head around indexing and list ranges/slicing. And no amount of Google-fu is helping me here.
Let's say I have a simple list:
list = ["a", "b", "c", "d"]
If I want to take a slice of the list, the end of the range is exclusive when using non-negative values:
print(list[0:1]) ['a']
Not super intuitive given my background, but fine. I understand what's going on and generally why it happens.
What I don't understand is why the end of the range is inclusive when using negative indexing:
print(list[0:-1]) ['a', 'b', 'c']
It seems that the slice should only include ['a', 'b']: given how Python indexes a range of non-negative values, I would expect the result to only give the list items up to (but not including) the penultimate item in the list.
What's the rationale behind an exclusive range for non-negative values but an inclusive range for negative values?
Videos
How does slicing on a NumPy array differ from slicing on standard Python lists or strings, in terms of both syntax and functionality?
How does slicing a string in Python differ from slicing a list, and what are some examples illustrating these differences?
What is the result of slicing a Python list with a negative step value, and how does it affect the sequence?
I was going through crash course and was learning slicing and i tried
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
it gave me ['charles', 'martina', 'michael']
shouldnt it give me ['michael', 'florence', 'eli']?
They both behave exactly the same.
For some reason you expect the list's indexes to start from 0 and
the string's indexes to start from 1.
The fact is that they both start at 0.
As @Mark suggested in the comments, strings and lists indexing from Python's documentation.
But it is the same!
Insert indices before each element:
[0=1,1=2,2=3,3=4][2:] means: from index 2 until end
[0=T,1=h,2=i,3=s,4= ,5=i,6=s,...][5:] means: from index 5( in this case from the letter 'i') until the end.