Skip to content

Arrays & Lists

1 min read2 lessons in topic

Learning objective

By the end of this lesson you will be able to use lists, understand indexing and know the cost of common operations.

An array is a collection of items stored one after another. Python's list is a flexible version of an array.

Indexing

Positions are counted from 0. In a Python list, courses[0] is the first item.

courses = ["Python", "Databases", "Networks"]
print(courses[0])   # Python
print(courses[-1])  # Networks (last item)
courses.append("Web Dev")
print(len(courses)) # 4

Common operations & their cost

  • Access by index — fast, O(1)
  • Append at the end — fast, O(1) on average
  • Search for a value — slow, O(n)
  • Insert or remove in the middle — O(n) because items shift

Big O describes how work grows with input size n. O(1) is constant time; O(n) grows linearly.

Code playground

Run and edit the starter code — experiment freely.

python · runnable

Output appears here when you press Run.

Practice exercise

Create a list of five courses. Print the second one, add a sixth, and print the total count.

Check your understanding

1. What is the index of the first element of a list?

2. How long does finding a value in an unsorted list take on average?

Self-graded — answer then check below.