Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Displaying values
Variables
The print function
List basics
17. Accessing list elements
Summary

Instruction

Excellent! Elements in a list are indexed (numbered), and you can get a specific item in the following way:

# define a list
canadian_cities = [
  'Toronto', 'Montreal', 'Calgary', 
  'Ottawa', 'Edmonton', 'Mississauga', 
  'Winnipeg', 'Vancouver', 'Brampton', 'Familton']

# access Ottawa
canadian_cities[3]

We provide list name (canadian_cities), followed by a pair of square brackets. Inside, we put the index (number) of the specific element. This way, we can access any element in the list.

List indices start at 0, not 1. In other words, canadian_cities[0] is the first element, and canadian_cities[3] – the fourth element. This is a common mistake among beginners, so watch out!

Exercise

What were the sales on the third day in your previously defined list? Access the right list element.

Stuck? Here's a hint!

Remember that the third element has index 2.