Reading preferences
Concept

Arrays and Lists

Picture numbered slots, then learn how to read, change, add, and remove items without confusing a position with a value.

array, list, sequence, index, append, insert, remove, mutation

Imagine numbered storage slots

Put three cards in a row: [5, 2, 4]. Each slot has a position, and each card has a value. In Python, slot labels start at zero:

Index (slot label)012
Value (card number)524

An index tells us where to look; a value tells us what is there. Index 1 is not the same thing as the number 1.

Why keep values in a sequence

A sequence keeps items in a particular order. Instead of inventing a separate variable for every measurement, we can keep them together and use their positions. This makes it easier to visit, search, or sort them.

Lists are useful for a short collection of prices, a row of scores, or items you plan to process in order. Order is not automatically sorted order.

An array stores indexed items in a regular storage layout. A fixed-size array has a fixed number of slots. A dynamic array can resize its backing storage as items are added.

Python's built-in list is a resizable, array-like sequence of references to objects. It is not a fixed-size array, and it can hold different types of objects. For these examples, use ordinary numbers so the steps are easy to follow. A Python list is also not a linked list.

Read and change one item

Reading values[1] gets the second item. Setting values[1] = 8 changes that item without adding a new slot. With three items, the non-negative valid indices are 0, 1, and 2, not 3. len(values) gives the number of items.

Python also permits negative indices: values[-1] means the last item. Reading an index outside the available slots raises IndexError; an empty list has no first or last item.

Add and remove items

OperationMeaningImportant detail
append(value)Add at the endDoes not sort the list
insert(index, value)Make room at a positionLater items shift right
pop(index)Remove and return that slot's itemLater items shift left
remove(value)Remove the first matching valueRaises ValueError if absent
values[index] = valueReplace an existing itemLength stays the same

Python illustration

values = [5, 2, 4]
print(values[1])
values[1] = 8
values.append(1)
values.insert(0, 3)
removed = values.pop(2)
values.remove(1)
print(removed)
print(values)

Prepared output — not live execution:

2
8
[3, 5, 4]

After insertion, the list is [3, 5, 8, 4, 1]. pop(2) removes the value 8, not the value 2. Removing 1 then leaves [3, 5, 4]. Each step changes the same list; this is called mutation.

What work do these operations need

For a Python list, reading or replacing one valid slot takes O(1) time: the amount of work does not grow with list length. Inserting or removing near the front can take O(n), because many references shift.

Appending is O(1) amortised: it is constant on average across many appends, but an individual resize can take O(n). Removing by value also needs to search. These claims describe Python's list implementation, not every structure called a list. Learn the notation in time and space complexity.

Common mistakes

Do not use the value as if it were an index. Do not assume adding an item sorts the sequence. Also, other = values makes a second name for the same list, not an independent copy. For these flat examples, values.copy() copies the outer list; nested objects would still be shared.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. In [5, 2, 4], what is at Python index 1?
2. What does values[0] = 9 do to [5, 2, 4]?
3. Why can inserting at the front take more work than changing one existing slot?
4. What does Python remove(2) remove from [2, 7, 2]?

References and further reading