Python lists
grouping objects into a collection of ordered and changeable items
2025-11-05 13:01
// updated 2025-11-06 19:02
// updated 2025-11-06 19:02
In Python, a list consists of:
- an ordered collection of items that can have different data types
- square brackets with items separated by commas
my_list = ['h', 90, True, 3.14, 'eX', False]In that list, we notice that our collection includes a couple of strings, an int, a float and two Booleans!
List methods
Similar to strings, we can use some built-in methods to learn about a list:
my_list = ['h', 90, True, 3.14, 'eX', False]
# access individual items by index
print(my_list[1])
# 90
# slicing the list
print(my_list[3:5])
# [3.14, 'eX']
# finding the length of a list
print(len(my_list))
# 6Changing list items
However, unlike strings, where we cannot changes individual characters, we can change individual list items:
my_list = ['h', 90, True, 3.14, 'eX', False]
# changing a list item by index
my_list[1] = 92
print(my_list)
# ['h', 92, True, 3.14, 'eX', False]Adding and removing list items
We can also add items to and remove items from the list with the help three built-in methods for lists:
append(item)remove(item)pop()andpop(atIndex)
my_list = ['h', 90, True, 3.14, 'eX', False]
# ==== USING append() ====
# adding list items with append
my_list.append('eX')
my_list.append(False)
my_list.append(2.718)
print(my_list)
# ['h', 90, True, 3.14, 'eX', False, 'eX', False, 2.718]
# ==== USING remove() ====
# remove the first item in the list
# ...that contains 'eX'
my_list.remove('eX')
print(my_list)
# ['h', 90, True, 3.14, False, 'eX', False, 2.718]
# ==== USING pop() ====
# remove the last item from the list
my_list.pop()
print(my_list)
# ['h', 90, True, 3.14, False, 'eX', False]
# remove the item at the given index
my_list.pop(1)
print(my_list)
# ['h', True, 3.14, False, 'eX', False]Pay close attention to what the remove method does, as it doesn't remove all instances of what we feed into the argument!
Summary
We can think of a list as:
- an ordered collection of one or more mutable (changeable) items:
- each referenced by an index
- the item can take on any data type
- capable of update by assignment
- denoted by square brackets
[ value1, ..., valueN ]
Applications
Great uses for lists include collections of:
- objects whose values change (often enough) but their order matters, e.g.:
- a queue of things to do and their completion rates
- a top X list of current hit songs
- a waiting list