11  Lists

A list datatype represents a numbered, ordered collection of items.

11.1 Characteristics

A list has square brackets ([]) on the extremities, and comma separated items inside.

A list may contain zero or more items. A list can contain items of any datatype, but as a best practice, all items in a list should share a datatype and structure.

my_empty_list = []
print(type(my_empty_list))
<class 'list'>
symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
print(type(symbols))
print(symbols)
<class 'list'>
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']

11.2 Accessing Items

We access items in a list by their numeric position, called the index. In Python (and most other languages), indices are zero-based, meaning the index of the first item is zero. We use square brackets ([]) to denote which item we would like to access:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]

print(symbols[0])
print(symbols[1])
print(symbols[2])
print(symbols[3])
print(symbols[4])
#print(symbols[5]) #> ERROR (IndexError)
MSFT
AAPL
GOOGL
AMZN
NFLX

We can use a negative one to dynamically reference the last item in the list (regardless of how many there are):

print(symbols[-1])
NFLX

11.2.1 List Slicing

It is possible to access a subset of the items in sequence, by denoting the numeric position of the first item (inclusive) and last item (exclusive):

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]

print(symbols[0:1]) # FROM 0 (INCLUSIVE) to 1 (EXCLUSIVE)
print(symbols[0:2]) # FROM 0 (INCLUSIVE) to 2 (EXCLUSIVE)
print(symbols[0:3]) # FROM 0 (INCLUSIVE) to 3 (EXCLUSIVE)
['MSFT']
['MSFT', 'AAPL']
['MSFT', 'AAPL', 'GOOGL']
print(symbols[2:4]) # FROM 2 (INCLUSIVE) to 4 (EXCLUSIVE)
['GOOGL', 'AMZN']
Explaining List Slicing

List slicing can be confusing at first, but here’s a simple way to think about it:

  • Left side of the colon:

    • This is the starting index.
    • The slice begins at this index, and includes the item at this position.
    • If you leave it blank, it starts from the beginning (index 0).
  • Right side of the colon:

    • This is the ending index.
    • The slice goes up to, but does not include, this index.
    • If you leave it blank, it goes up to the end of the list.

By default if we omit one of the sides, it will include all members to the start or end of the list:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]

print(symbols[ :2]) # FROM BEGINNING TO 2 (EXCLUSIVE)
print(symbols[2: ]) # FROM 2 (INCLUSIVE) TO THE END
['MSFT', 'AAPL']
['GOOGL', 'AMZN', 'NFLX']

Note: list slicing provides a subset of the items in sequence, however to access a subset of the items based on some condition, we will use a filter operation instead.

11.3 Operations

Here are some additional common list operations.

Mutating Operations

Many of these operations are mutating, which means they change the underlying object in-place.

11.3.1 Adding Items

Adding items, using the append method:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
print(symbols)

symbols.append("NVDA") # MUTATING
print(symbols)
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX', 'NVDA']

11.3.2 Updating Items

Updating an item, by its numeric position:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
print(symbols)

symbols[-1] = "UPDATED" # MUTATING
print(symbols)
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'UPDATED']

11.3.3 Removing Items

Removing a given item, using the remove method:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
print(symbols)

symbols.remove("NFLX") # MUTATING
print(symbols)
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
['MSFT', 'AAPL', 'GOOGL', 'AMZN']

Alternatively, removing an item by its numeric position, using the del keyword:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
print(symbols)

del symbols[-1] # MUTATING
print(symbols)
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
['MSFT', 'AAPL', 'GOOGL', 'AMZN']

11.3.4 Concatenating Lists

Concatenating two lists together:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
print(symbols)

all_symbols = symbols + ["SPOT", "NVDA"]
print(all_symbols)
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX', 'SPOT', 'NVDA']

11.3.5 Length Checking

We will commonly ask how many items a list contains, using the len function:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
len(symbols)
5

11.3.6 Inclusion Checking

We can use membership operators to check if an item is present in the list:

symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]

print("NFLX" in symbols)
print("NFLX" not in symbols)
True
False

11.4 Data Processing Operations

We will return to work with lists in much more detail, as we study list-based data processing techniques: