4.1. Lists#
Lists are one of Python’s most useful built-in type. In Python, a list is a sequence, meaning it is an ordered collection of items that can store multiple values in a single variable. Lists are extremely flexible because they can hold elements of different data types, such as numbers, strings, or even other lists, and they can be changed after creation by adding, removing, or modifying items. Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in a list are called elements.
Some basic properties of Python lists are as seen:
Property |
Description |
---|---|
Ordered |
Elements maintain the order in which they were added and can be accessed by index (starting at 0). |
Mutable |
You can change, add, or remove elements after the list is created. |
Allows Duplicates |
Lists can contain repeated values without restriction. |
Dynamic Size |
The length of a list can grow or shrink as elements are added or removed. |
Heterogeneous |
A single list can hold items of different data types (e.g., integers, strings, objects). |
4.1.1. Basic Operations#
Basic Python list operations include:
Creating lists
Indexing (mylist[0], mylist[-1])
Slicing (mylist[1:4], mylist[::-1])
Length with len()
Iterating through lists (for loops, while loops)
4.1.1.1. Creating Lists#
The simplest to create a list is to enclose the elements in square brackets ([
and ]
) and we usually assign the created list to a variable name. For example, here is a list of two integers:
numbers = [ 1, 2, 3, 4, 5 ]
And here’s a list of three strings:
fruits = [ 'apple', 'banana', 'cherry']
The elements of a list don’t have to be the same type. The following list contains a string, a float, an integer, and even another list. This list is therefore heterogeneous:
t = ['spam', 2.0, 5, [10, 20]]
A list within another list is called a nested list, which are important because they let you represent more complex data structures, such as tables, matrices, and provide a stepping stone to understanding multi-dimensional arrays for advanced data science processing.
A list that contains no elements is called an empty list, which can be created with empty brackets, []
.
empty = []
4.1.1.2. Indexing#
In Python, list indexing is the process of accessing individual elements within a list using their position. List indices work as:
Python uses zero-based indexing, which means the first element has index 0, the second has index 1, and so on.
Any integer expression can be used as an index.
If you try to read or write an element that does not exist, you get an
IndexError
.If an index has a negative value, it counts backward from the end of the list beginning with
-1
.
With 0-based indexing and negative indexing, list indexing looks like figure below.

Fig. 4.1 Python List Indexing #
To access an element of a list, we can use the bracket operator. The index of the first element is 0
.
fruits = [ "apple", "banana", "cherry" ]
fruits[0] ### 'apple'
Although a list can contain another list, the nested list still counts as a single element – so in the following list, there are only four elements.
t = ['spam', 2.0, 5, [10, 20]]
len(t) ### 4
4.1.1.3. Modifying Elements#
Unlike strings, lists are mutable. When the bracket operator appears on the left side of an assignment, it identifies the element of the list that will be assigned.
fruits = [ "apple", "banana", "cherry" ]
fruits[0] = "avocado"
fruits[0] ### is now 'avocado'
4.1.1.4. List Slicing#
Things to know about list slicing:
Slicing makes a new list from a portion of the elements of the original ist.
The list slicing syntax uses colon(s) and index parameters inside the square brackets following the list variable name; for example,
The start index is inclusive but the stop index in exclusive; for example, nums[1:3]. would return a new list containing the elements from index[1] (inclusive) to index[3] (exclusive).
There are 3 index parameters in the square brackets and they are all optional: start, stop, step. Although we mostly only use the start index and stop index.
The following example selects the second and third elements from a list of four letters.
letters = ['a', 'b', 'c', 'd']
letters[1:3] ### ['b', 'c']
If you omit the first index, the slice starts at the beginning.
letters[:2] ### ['a', 'b']
If you omit the second, the slice goes to the end.
letters[2:] ### ['c', 'd']
So if you omit both, the slice is a copy of the whole list.
letters[:] ### ['a', 'b', 'c', 'd']
Because list
is the name of a built-in function, you should avoid using it as a variable name.
4.1.1.5. List Functions#
Some Python built-in functions are useful when working with lists:
len()
min()
max()
sum()
The len
function returns the length of a list.
cheeses = ['Cheddar', 'Edam', 'Gouda']
len(cheeses)
The length of an empty list is 0
.
>>> empty = []
>>> empty
[]
>>> len(empty)
0
No other mathematical operators work with lists, but the built-in function sum
adds up the elements.
t1 = [1, 2]
sum(t1) ### 3
And min
and max
find the smallest and largest elements.
t1 = [1, 2]
min(t1) ### 1
t1 = [1, 2]
max(t2) ### 2
4.1.1.6. List Methods#
Python also has a set of built-in methods that you can use on lists. Commonly used list methods include:
len()
sum()
minx()
max()
append()
extend()
pop()
remove()
4.1.1.6.1. The Dot Notation#
Use the dot notation (.
), you can show the methods a list object has. Just type the name of the list plus .
and hit the Tab key twice:
>>> nums = [ 1, 2, 3 ]
>>> nums.
t1.append( t1.copy() t1.extend( t1.insert( t1.remove( t1.sort(
t1.clear() t1.count( t1.index( t1.pop( t1.reverse()
4.1.1.6.2. append#
Python provides methods that operate on lists. For example, append
adds a new element to the end of a list:
>>> letters = ['a', 'b', 'c', 'd']
>>> letters
['a', 'b', 'c', 'd']
>>> letters.append("e")
>>> letters
['a', 'b', 'c', 'd', 'e']
letters ### ['a', 'b', 'c', 'd', 'e']
4.1.1.6.3. extend#
extend
takes a list as an argument (not values) and appends all of the elements to the original list:
>>> letters.extend('f', 'g')
Traceback (most recent call last):
File "<python-input-33>", line 1, in <module>
letters.extend('f', 'g')
~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: list.extend() takes exactly one argument (2 given)
letters ### ['a', 'b', 'c', 'd', 'e']
letters.extend(['f', 'g'])
letters ### ['a', 'b', 'c', 'd', 'e', 'f', 'g']
4.1.1.6.4. pop#
There are two methods that remove elements from a list: pop() and remove()
If you know the index of the element you want, you can use pop
:
If you provide an index as the argument, pop() removes and returns the element at that index.
If you do not provide an index as argument, pop() remove and return the last element in the list.
>>> nums = [1, 2, 3, 4 ,5]
>>> nums.pop(1) ### pop(1) removes and returns the 2nd element in list.
2
>>> nums
[1, 3, 4, 5]
>>> nums.pop() ### pop() removes & returns the last element.
5
>>> nums
[1, 3, 4]
>>>
4.1.1.6.5. remove#
On the other hand, if you know the element you want to remove (but not the index), you can use remove
:
>>> nums = [1, 2, 3, 4 ,5]
>>> nums.remove(3)
>>> nums
[1, 2, 4, 5]
If the element you ask for is not in the list, that’s a ValueError.
>>> nums
[1, 2, 4, 5]
>>> nums.remove(6)
Traceback (most recent call last):
File "<python-input-48>", line 1, in <module>
nums.remove(6)
~~~~~~~~~~~^^^
ValueError: list.remove(x): x not in list
4.1.2. List operations#
List operators like in
, +
, and *
are used on lists as well.
The in
operator works on lists – it checks whether a given element appears anywhere in the list.
fruits = [ "apple", "banana", "cherry" ]
'banana' in fruits ### True
And 10
is not considered to be an element of t
because it is an element of a nested list, not t
.
t = ['spam', 2.0, 5, [10, 20]]
10 in t ### False
The +
operator concatenates lists.
t1 = [1, 2]
t2 = [3, 4]
t1 + t2 ### [1, 2, 3, 4]
The *
operator repeats a list a given number of times.
['spam'] * 4 ### ['spam', 'spam', 'spam', 'spam']
[ 1, 2, 3 ] * 3 ### [1, 2, 3, 1, 2, 3, 1, 2, 3]
4.1.3. Lists and strings#
A string is a sequence of characters and a list is a sequence of values,
but a list of characters is not the same as a string.
To convert from a string to a list of characters, you can use the list
function.
>>> s = 'spam'
>>> t = list(s)
>>> t
['s', 'p', 'a', 'm']
>>>
The list
function breaks a string into individual letters.
If you want to break a string into words, you can use the split
method:
>>> s = 'pining for the fjords'
>>> t = s.split()
>>> t
['pining', 'for', 'the', 'fjords']
The list function breaks a string into individual letters. If you want to break a string into words, you can use the split
method:
>>> s = 'pining for the fjords'
>>> t = s.split()
>>> t
['pining', 'for', 'the', 'fjords']
An optional argument called a delimiter specifies which characters to use as word boundaries. The following example uses a hyphen as a delimiter.
>>> s = 'ex-parrot'
>>> t = s.split('-') ### note that this returns a list
>>> t
['ex', 'parrot']
If you have a list of strings, you can concatenate them into a single string using join
.
join
is a string method, so you have to invoke it on the delimiter and pass the list as an argument.
>>> delimiter = ' '
>>> t = ['pining', 'for', 'the', 'fjords']
>>> s = delimiter.join(t)
>>> s
'pining for the fjords'
In this case the delimiter is a space character, so join
puts a space
between words. To join strings without spaces, you can use the empty string, ''
, as a delimiter.
4.1.4. Sorting lists#
Python provides a built-in function called sorted
that sorts the elements of a list.
>>> scramble = ['c', 'a', 'b']
>>> sorted(scramble)
['a', 'b', 'c']
The original list is unchanged.
>>> scramble
['c', 'a', 'b']
sorted
works with any kind of sequence, not just lists. So we can sort the letters in a string like this.
4.1.5. Objects and Values#
If we run these assignment statements:
a = 'banana'
b = 'banana'
We know that a
and b
both refer to a string, but we don’t know whether they refer to the same string. There are two possible states, shown in the following figure.
>>> a = 'banana'
>>> b = 'banana'
>>> a == b
True
>>> a is b
True
In this example, Python only created one string object, and both a
and b
refer to it. But when you create two lists, you get two objects.
>>> a = [ 1, 2, 3 ]
>>> b = [ 1, 2 ,3 ]
>>> a == b
True
>>> a is b
False
>>>
In this case we would say that the two lists are equivalent, because they have the same elements, but not identical, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily identical.
4.1.6. Aliasing#
If num_a
refers to an object and you assign num_b = num_a
, then both variables refer to the same object.
>>> nums_a = [ 1, 2, 3, 4, 5 ]
>>> nums_b = nums_a
>>> nums_a == nums_b
True
>>> nums_a is nums_b
True
The association of a variable with an object is called a reference. In this example, there are two references to the same object.
An object with more than one reference has more than one name, so we say the object is aliased. If the aliased object is mutable, changes made with one name affect the other. In this example, if we change the object nums_a
refers to, we are also changing the object nums_a
refers to.
>>> nums_a = [ 1, 2, 3, 4, 5 ]
>>> nums_a[0] = 1000
>>> nums_a
[1000, 2, 3, 4, 5]
4.1.7. Glossary#
list: An object that contains a sequence of values.
element: One of the values in a list or other sequence.
nested list: A list that is an element of another list.
delimiter: A character or string used to indicate where a string should be split.
equivalent: Having the same value.
identical: Being the same object (which implies equivalence).
reference: The association between a variable and its value.
aliased: If there is more than one variable that refers to an object, the object is aliased.
attribute: One of the named values associated with an object.
4.1.8. Exercises#
4.1.8.1. Exercise#
John wants to print an ASCII art. He got a cat art from here and he is thinking of using a list data type and a for loop to print the cat. Help John out! (# You may need to use an escape sequence to instruct Python to treat certain special characters or symbols as a string.
cat = [
|\---/|
| o_o |
\_^_/
]
for i in cat:
print(i)