Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Indexing and Slicing

Give Feedback

In this lesson, you’ll see how to access individual elements and sequences of objects within your lists. Lists elements can be accessed using a numerical index in square brackets:

>>>
>>> mylist[m]

This is the same technique that is used to access individual characters in a string. List indexing is also zero-based:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']

>>> a[0]
'spam'
>>> a[2]
'bacon'
>>> a[5]
'lobster'
>>> a[len(a)-1]
'lobster'
>>> a[6]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    a[6]
IndexError: list index out of range

List elements can also be accessed using a negative list index, which counts from the end of the list:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']

>>> a[-1]
'lobster'
>>> a[-2]
'ham'
>>> a[-5]
'egg'
>>> a[-6]
'spam'
>>> a[-len(a)]
'spam'
>>> a[-8]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    a[-8]
IndexError: list index out of range

Slicing is indexing syntax that extracts a portion from a list. If a is a list, then a[m:n] returns the portion of a:

  • Starting with postion m
  • Up to but not including n
  • Negative indexing can also be used

Here’s an example:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[2:5]
['bacon', 'tomato', 'ham']
>>> a[-5:-2]
['egg', 'bacon', 'tomato']
>>> a[1:4]
['egg', 'bacon', 'tomato']
>>> a[-5:-2] == a[1:4]
True

Omitting the first and/or last index:

  • Omitting the first index a[:n] starts the slice at the beginning of the list.
  • Omitting the last index a[m:] extends the slice from the first index m to the end of the list.
  • Omitting both indexes a[:] returns a copy of the entire list, but unlike with a string, it’s a copy, not a reference to the same object.

Here’s an example:

>>>
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[:4]
['spam', 'egg', 'bacon', 'tomato']
>>> a[0:4]
['spam', 'egg', 'bacon', 'tomato']
>>> a[2:]
['bacon', 'tomato', 'ham', 'lobster']
>>> a[2:len(a)]
['bacon', 'tomato', 'ham', 'lobster']

>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[:]
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a == a[:]
True
>>> a is a[:]
False

>>> s = 'mybacon'
>>> s[:]
'mybacon'
>>> s == s[:]
True
>>> s is s[:]
True

A stride can be added to your slice notation. Using an additional : and a third index designates a stride (also called a step) in your slice notation. The stride can be either postive or negative:

>>>
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[0:6:2]
['spam', 'bacon', 'ham']
>>> a[1:6:2]
['egg', 'tomato', 'lobster']
>>> a[6:0:-2]
['lobster', 'tomato', 'egg']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[::-1]
['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']

Comments & Discussion

JulianV on Dec. 13, 2019

Nice!

Orlando Uribe on March 8, 2020

Thanks for this material. I guess there is a typo error (??) in one of the stride written examples (video example is OK):

Typo error:

>>> a[0:6:-2]
['lobster', 'tomato, 'egg']

the correct statement should be:

>>> a[6:0:-2]
['lobster', 'tomato, 'egg']

otherwise,

>>> a[0:6:-2]
[]

Chris Bailey RP Team on March 8, 2020

Hi @Orlando Uribe, You’re correct, I will get that changed to match the video lesson. Thanks

Ajay on June 3, 2020

@chris Video is very informative, thanks for this. When i was practising examples, got one doubt:

In [43]: a = [9, 1, 2, 4, 5, 6, 8]

In [44]: a[6:0:-2]
Out[44]: [8, 5, 2]

With negative striding why 9 is not included.

Does it exclude last value?

Chris Bailey RP Team on June 3, 2020

Hi @Ajay,

Thanks for watching the course! The slice you have created would start at index 6 and end at index 1 and not include index 0, which is why you are not getting index 0 of 9. If you were to use:

>>> a = [9, 1, 2, 4, 5, 6, 8]

>>> a[6::-2]
[8, 5, 2, 9]

That would include index 0. I hope this helps to explain what’s happening. Reiterating from the text below the lesson.

Slicing is indexing syntax that extracts a portion from a list. If a is a list, then a[m:n] returns the portion of a:

  • Starting with postion m
  • Up to but not including n

Ajay on June 4, 2020

@chris thanks for clarification.

Become a Member to join the conversation.