Python Lists

A list is Python's built-in container for holding an ordered, changeable collection of items in a single variable.

What is a list?

A list stores several values together under one name. You create one by placing comma-separated values inside square brackets. Lists keep the items in the order you write them, and that order stays fixed until you decide to change it, which makes lists a natural fit for anything that has a sequence, such as steps in a recipe or scores in a game.

Creating a list

fruits = ["apple", "banana", "cherry"]
print(fruits)
print(type(fruits))

The example above prints the whole list and then confirms its type. Running it shows ['apple', 'banana', 'cherry'] followed by <class 'list'>, so you can see that Python treats the collection as a single object of type list.

Key characteristics

  • Ordered: items have a fixed position, and new items are added at the end by default.
  • Changeable (mutable): you can add, replace, or delete items after the list is created.
  • Allows duplicates: the same value can appear more than once because items are stored by position, not by uniqueness.
  • Mixed types: a single list can hold strings, numbers, booleans, and even other lists at the same time.

Mixed data and duplicates

mixed = ["text", 42, True, 3.14, "text"]
print(mixed)
print(len(mixed))

Here the list mixes four different data types and repeats the value 'text'. The built-in len() function returns 5, counting every item regardless of type or repetition.

Note: You can also build a list from other data using the list() constructor, for example list(("a", "b", "c")) turns a tuple into a list.
FunctionPurposeExample result
len()Count how many items are in the listlen([1, 2, 3]) is 3
list()Build a list from another iterablelist("ab") is ['a', 'b']
type()Confirm the object is a list<class 'list'>

Exercise: Python Lists

What is the key difference between a Python list and a tuple?