Skip to main content

Lesson 11: What Are Lists?

Learn Python lists: how to create them, access items by index, understand zero-based counting, use negative indexes, find length with len(), and when to use lists vs variables.

Introduction

You're managing a project with 50 rooms. You need to store their names in your script.

You could create 50 separate variables:

room_1 = "Office 1"
room_2 = "Office 2"
room_3 = "Office 3"
# ... 47 more lines of this

Or you could use a list:

rooms = ["Office 1", "Office 2", "Office 3", ...]

The difference is night and day. The first approach doesn't scale. The second does.

Lists store multiple items in one container. If you've used selection sets in Revit or worked with layer lists in CAD, you already understand the concept. Python lists work the same way — they're organised collections you can reference and manipulate as a group.


Lesson Overview

This section contains a general overview of topics you will learn in this lesson.

  • What lists are and how to create them
  • Accessing items by position (index)
  • Why Python starts counting at 0
  • Using negative indexes to count from the end
  • Finding list length with len()
  • When to use lists vs individual variables

Creating a List

Lists are created using square brackets, with items separated by commas.

floors = ["Basement", "Ground", "First", "Second"]
sheets = ["A-101", "A-102", "A-201", "A-202"]
areas = [450.5, 380.2, 520.8, 410.3]

Each of these is a single variable holding multiple values. Instead of managing floor_1, floor_2, floor_3 separately, you have one floors list with everything organised inside.

You can also create an empty list and fill it later:

rooms = []

We'll cover adding items in the next lesson.


Accessing Items by Index

Each item in a list has a position called an index. This is where things get a bit unusual if you're new to programming.

Python starts counting at 0, not 1.

sheets = ["A-101", "A-102", "A-201", "A-202"]

To access items:

  • sheets[0] gives you "A-101" (first item)
  • sheets[1] gives you "A-102" (second item)
  • sheets[2] gives you "A-201" (third item)
  • sheets[3] gives you "A-202" (fourth item)

This zero-based indexing feels strange at first, but it's standard across virtually all programming languages. It's rooted in how computer memory works. You'll adjust to it quickly through practice.


Negative Indexing

Python also lets you count backwards from the end using negative numbers.

sheets = ["A-101", "A-102", "A-201", "A-202"]

last_sheet = sheets[-1]      # "A-202"
second_last = sheets[-2]     # "A-201"

This is particularly useful when you need the last item in a list but don't know (or don't want to calculate) how long the list is.


Finding List Length

Use len() to find how many items are in a list.

rooms = ["Office 1", "Office 2", "Conference", "Break Room"]
room_count = len(rooms)
print(room_count)  # Output: 4

This works for any list, regardless of what's inside.

arch_sheets = ["A-101", "A-102", "A-201"]
struct_sheets = ["S-101", "S-102"]
mep_sheets = ["M-101", "E-101"]

total_sheets = len(arch_sheets) + len(struct_sheets) + len(mep_sheets)
print(f"Total sheets: {total_sheets}")  # Output: Total sheets: 7

Why Lists Matter

Compare these two approaches:

Without lists:

room_1 = "Office 1"
room_2 = "Office 2"
room_3 = "Office 3"
# ... and so on

What happens when you need to add a room? You create another variable. What if you need to rename all of them? You update each one manually. What if you have 500 rooms? This approach breaks down completely.

With lists:

rooms = ["Office 1", "Office 2", "Office 3"]

Adding a room becomes a single operation (next lesson). Processing all rooms becomes possible (Module 05 on loops). The logic doesn't change whether you have 3 rooms or 300.

Lists scale. Individual variables don't.


Real Examples

Floor Names

floors = ["Basement", "Ground", "First", "Second", "Third"]

# Access first floor
first_floor = floors[0]
print(first_floor)  # Output: Basement

# Access top floor
top_floor = floors[-1]
print(top_floor)  # Output: Third

# Count floors
floor_count = len(floors)
print(f"Building has {floor_count} floors")  # Output: Building has 5 floors

Sheet Numbers

sheets = ["A-101", "A-102", "A-201", "A-202", "S-101", "S-102"]

# First sheet
print(sheets[0])  # Output: A-101

# Last sheet
print(sheets[-1])  # Output: S-102

# Total count
print(len(sheets))  # Output: 6

Room Areas

areas = [450.5, 380.2, 520.8, 410.3, 395.7]

# First room
print(areas[0])  # Output: 450.5

# Last room
print(areas[-1])  # Output: 395.7

# Count
print(len(areas))  # Output: 5

Common Mistakes

Forgetting Python starts at 0

This trips up everyone initially.

sheets = ["A-101", "A-102", "A-201"]

first_sheet = sheets[1]
print(first_sheet)  # Output: A-102 (not what you wanted!)

# Correct
first_sheet = sheets[0]
print(first_sheet)  # Output: A-101

The first item is always at index 0. You'll internalize this with practice.


Index out of range

rooms = ["Office 1", "Office 2", "Office 3"]

# This list has 3 items: indexes 0, 1, 2
# There is no index 3

print(rooms[3])
# Error: IndexError: list index out of range

If a list has 3 items, the valid indexes are 0, 1, and 2. The last index is always len(list) - 1.

Alternatively, use negative indexing to avoid this:

last_room = rooms[-1]  # Always gets the last item, regardless of length

Using the wrong brackets

# Wrong - these create different data types
sheets = ("A-101", "A-102")  # Parentheses create a tuple
sheets = {"A-101", "A-102"}  # Curly braces create a set

# Correct - square brackets create a list
sheets = ["A-101", "A-102"]

Lists always use square brackets [].


When to Use Lists

Use lists when you have:

  • Multiple items of the same type
  • Data that might grow or shrink
  • Items you need to process as a group

Examples:

  • Room names, areas, or types
  • Sheet numbers
  • Floor names
  • Element IDs
  • View names

Don't use lists for:

  • Unrelated values that don't naturally group together
# Avoid this
project_info = ["Community Center", 12, True, 450000]

# Better
project_name = "Community Center"
floor_count = 12
is_approved = True
budget = 450000

Later in the course, you'll learn about dictionaries (Module 07), which handle mixed-type data more elegantly.


Assignment

  1. Create a new file called lists_practice.py
  2. Create three lists:
    • Floor names: ["Basement", "Ground", "First", "Second", "Third"]
    • Sheet numbers: ["A-101", "A-102", "A-201", "A-202"]
    • Room areas: [450.5, 380.2, 520.8, 410.3]
  3. For each list, print:
    • The first item (index 0)
    • The last item (using negative indexing)
    • The total number of items (using len())
  4. Access the third item in the floors list. Remember: the third item is at index 2.
  5. Experiment with errors:
    • Try accessing sheets[10] on your 4-item list
    • Read the error message
    • Try accessing sheets[-10]
    • Understanding these errors now will help you debug later
  6. Create an empty list called rooms and print its length. Verify it returns 0.
  7. Real scenario practice:
    • Given this list: ["A-101", "A-102", "A-201", "A-202", "S-101"]
    • Print the total number of sheets
    • Print the last architectural sheet (at index 3)
    • Print the first structural sheet (at index 4)

Knowledge Check

The following questions are an opportunity to reflect on key topics in this lesson.


Additional Resources

This section contains helpful links to related content. It isn't required, so consider it supplemental.

Updated on Mar 27, 2026