132.1 Lists

Learn how to use lists to store and manipulate multiple values in a single variable.

What is a list?

A list is a data structure that stores a sequence of values. In Python, a list is implemented as a dynamic array, which means it can change size during execution and store items of any data type, even mixed types within the same list.

In most programming languages, an array is a fixed-size structure that stores elements of only one type (e.g. integers or floats). Python lists are more flexible than traditional arrays because:

  • They are mutable (can be changed after creation)

  • They can grow and shrink dynamically

  • They can store different types of values together (e.g. strings, numbers, Booleans)

SET example_list TO LIST ["Alice", 10, TRUE]

This makes Python lists powerful and convenient for general-purpose programming, though they may be less efficient than typed arrays for large-scale numerical operations.

Creating a list

In Python, you create a list using square brackets. In pseudocode, we use a LIST declaration with an initial size or contents.

SET names TO LIST ["Lina", "Ari", "Noah"]

Accessing and modifying list items

Each item in a list can be accessed using its index. Indices usually start at 0 in most languages, including Python.

SET names TO LIST ["Lina", "Ari", "Noah"]
OUTPUT names[0]         // Lina
SET names[1] TO "Aria"  // LIST ["Lina", "Aria", "Noah"]

Adding and removing items

Lists are dynamic, which means you can append, insert, or remove elements at runtime.

SET names TO LIST ["Lina", "Ari", "Noah"]
APPEND "Zane" TO names               // LIST ["Lina", "Ari", "Noah", "Zane"]
INSERT "Kai" AT POSITION 1 IN names. // LIST ["Lina", "Kai", "Ari", "Noah", "Zane"]
REMOVE "Lina" FROM names             // LIST ["Kai", "Ari", "Noah", "Zane"]

Looping through a list

Lists are often used with loops to process each element.

FOR name IN names
    OUTPUT name
ENDFOR

Useful list functions and methods

Task
Python
Pseudocode

Find length

len(names)

LENGTH(names)

Check membership

"Kai" in names

"Kai" IN names

Sort items

names.sort()

SORT names

Reverse items

names.reverse()

REVERSE names

Key concepts

  • A list stores multiple values in one variable and can be modified during runtime.

  • List items are accessed using an index.

  • Lists support operations like append, insert, remove, and iteration.

  • Lists are essential for storing dynamic collections such as user inputs or game scores.

Last updated

Was this helpful?