Skip to content

Python Lists

Create, read, change, sort, and safely copy lists of values.

12 min read 10 quiz questions

A list holds a sequence

A list stores values in order. Put items between square brackets, separated by commas.

Positions begin at 0. A negative position counts from the end.

Example 1: create, index, and slice a list
colors = ['red', 'green', 'blue']
print(colors[0])
print(colors[-1])
print(colors[1:])

colors[0] is the first item. colors[-1] is the last item. The slice colors[1:] makes a new list from index 1 to the end.

Change a list

Lists are mutable, which means you can change them after creating them. You can assign to an index or use list methods.

Example 2: add and remove list items
pets = ['cat', 'dog']
pets[1] = 'parrot'
pets.append('fish')
pets.insert(1, 'hamster')
pets.extend(['lizard', 'ant'])
pets.remove('cat')
last_pet = pets.pop()
del pets[1]
print(pets)
print(last_pet)

append() adds one item, insert() adds at an index, and extend() adds every item from another sequence. remove() removes a matching value, pop() removes and returns an item, and del removes by index.

Order and copy lists

Example 3: sort, reverse, and copy
scores = [8, 3, 5]
print(sorted(scores))
scores.sort()
scores.reverse()
copy_scores = scores.copy()
copy_scores.append(10)
print(scores)
print(copy_scores)
print(len(scores))
ToolWhat it doesChanges the list?
sorted(items)Returns a sorted new listNo
items.sort()Sorts the same listYes
items.reverse()Reverses the same listYes
len(items)Counts itemsNo

Key points

  • Lists use square brackets.
  • Indexes start at zero; -1 is the last item.
  • Lists can be changed with assignments and methods.
  • sorted() returns a new list, while sort() changes one.
  • Use list.copy() to avoid accidental aliasing.

Worked example

A Saturday market list grows and shrinks: add coriander, buy the aubergine off, count what is left.

Example
basket = ["rice", "aubergine"]
basket.append("coriander")
basket.remove("aubergine")
print(basket, len(basket))

How it works, step by step

  1. append() adds to the END.
  2. remove() deletes the first matching VALUE (not position).
  3. len() counts current items.

References & Further Reading

Related entries from PyLabs's own reference library.

  • copy() (Make a shallow copy of a list.)
  • append() (Add a single item to the end of the list.)
  • pop() (Remove and return the item at the specified index.)

Your notes

Sign in to keep private notes alongside this lesson.

Sign in to take notes

Practice exercise

Not passed yet

Create numbers = [4, 1, 3]. Append 2, sort the list in place, then print it.

Expected output: [1, 2, 3, 4]

# Write your code below
Want another challenge on this module? More coding practice

Check yourself

Python Lists quiz

10 questions. You get instant feedback per question, and the timer starts when you press the button.

Lesson incomplete

Not completed

Create an ID or sign in to save lesson completion across devices.

Create ID to save
Module lesson progress0/5 complete
Module cheat sheet