PyPyPath

Module 5: Lists in Python

Indexing Lists

List indexing works just like strings: position 0 is the first item. Negative indexes count from the end.

About 8 minutes

Example code
colors = ["red", "green", "blue", "yellow"]
print(colors[0])   # red
print(colors[2])   # blue
print(colors[-1])  # yellow (last)
Indexes in colors list

red

[0]

green

[1]

blue

[2]

yellow

[3]

Practice

From cities list, print first city, third city, and last city.

Key takeaways

  • First item is index 0.
  • Use list[i] to access one item.
  • -1 is the last item.

Quick check: Indexing Lists

Question 1 of 2

What is ["x","y","z"][0]?