Indexing and Slicing in Python

Indexing and Slicing are basic operations in Python with the help of which you can extract items from inside of an ordered sequence.

Because strings are defined as ordered sequence of characters, we will use them to explain the operations. The knowledge gained here will apply to other ordered sequences in python like lists.

To extract the items, you need to provide positional offset into the sequence. The offset start at 0 from left and at -1 from right.


S = 'Python'

P y t h o n
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1

Indexing

In Python, individual character in a string can be fetched by indexing - providing the numeric offset of the desired component in square brackets after the string. You get back the one character string at the specified position.

>>> S[3],S[-2]
('h', 'o')

Technically, a negative offset is added to the length of a string to derive a positive offset.

More mathematically minded readers sometimes detect a small asymmetry here: the leftmost item is at offset 0, but the rightmost is at offset −1. Alas, there is no such thing as a distinct −0 value in Python. Hence S[0] = S[-0] and will return 'P' only.

Indexing (S[i]) fetches component at offset:
  • The first item is at offset 0.
  • Negative indexes mean to count backward from the end or right starting at -1.
  • S[0] fetches the first item.
  • S[−2] fetches the second item from the end (like S[len(S)−2]).

Slicing

Slicing, a generalized form of indexing that returns an entire section, not a single item. Probably the best way to think of slicing is that it is a type of parsing (analysing structure), especially when applied to strings - it allows us to extract an entire section (sub-string) in a single step.

The basics of slicing are straightforward. When you index a sequence object such as a string on a pair of offsets separated by a colon, Python returns a new object containing the contiguous section identified by the offset pair. The left offset is taken to be the lower bound (inclusive), and the right offset is taken to be the upper bound (non-inclusive). That is, Python fetches all items from the lower bound up to but not including the upper bound, and returns a new object containing the fetched items. If omitted, the left and right bounds default to 0 and the length of the object you are slicing, respectively.

>>> S[1:4],S[-4:-1]
('yth', 'tho')


Slicing (S[i:j]) extracts contiguous sections of ordered sequences:
  • The upper bound is non-inclusive.
  • Slice boundaries default to 0 and the sequence length, if omitted.
  • S[1:3] fetches items at offsets 1 up to but not including 3.
  • S[1:] fetches items at offset 1 through the end (the sequence length).
  • S[:3] fetches items at offset 0 up to but not including 3.
  • S[:−1] fetches items at offset 0 up to but not including the last item.
  • S[:] fetches items at offsets 0 through the end - making a top-level copy of S. This seems to be a very common technique: It makes a full top-level copy of a sequence object —> an object with the same value, but a distinct piece of memory. This isn’t very useful for immutable objects like strings, but it comes in handy for objects that may be changed in place, such as lists.

Extended slicing (S[i:j:k]) accepts a step (or stride) k, which defaults to +1:
  • Allows for skipping items and reversing order.

Use a negative stride to collect items in the opposite order. For example, the slicing expression "hello"[::−1] returns the new string "olleh" - the first two bounds default to 0 and the length of the sequence, as before, and a stride of −1 indicates that the slice should go from right to left instead of the usual left to right. The effect, therefore, is to reverse the sequence.

With a negative stride, the meanings of the first two bounds are essentially reversed.
That is, the slice S[5:1:−1] fetches the items from 2 to 5, in reverse order (the result starts from offset 5 and contains items from offsets 5, 4, 3, and 2).

Simply put, positive offset means position from left (start at 0) and negative offset means position from right (start at -1). First optional input (defaults to 0) means lower bound (inclusive) and second optional input (defaults to Object length - No. of items in the sequence/collection) means upper bound (non-inclusive). The third optional input (defaults to +1) is called stride. Positive stride means move from Left to Right and Negative stride means move from Right to Left. The value of stride is used to fetch the next item.

Hence slicing is like <Ordered Sequence Object>[start offset:end offset:move direction]

>>> S = 'Python'
>>>
>>> S[2:4:1]
'th'
>>> S[-2:-4:1]
''
>>> S[-4:-2:1]
'th'
>>> S[1:-2:1]
'yth'
>>> S[5:2:-1]
'noh'
>>> S[5:2:1]
''
>>>
>>> S[5:1:-2]
'nh'
>>>
>>> S[6]
Traceback (most recent call last):
  File "<pyshell#133>", line 1, in <module>
    S[6]
IndexError: string index out of range



One more point is that if the ordered sequence is a mutable object in Python, like list, then you can not only extract the parts of the sequence but can also assign new items at specified locations.

>>> S = ['P','y','t','h','o','n']
>>>
>>> S
['P', 'y', 't', 'h', 'o', 'n']
>>>
>>> S[2:4] = ['R','X']

>>> 
>>> S
['P', 'y', 'R', 'X', 'o', 'n']

Comments

Back To Top

Popular posts from this blog

How to save video from Internet Explorer

error 18 at 0 depth lookup: self signed certificate

How to check fragmentation in MySQL tables