Lists in Python
Now that we've covered the basic data types in Python, let's start covering the built-in data
structures. First, we have lists.
To define a list, we use square brackets [] with the elements separated by a comma.
💡 Tip: It's recommended to add a space after each comma to make the code more readable.
For example, here we have examples of lists:
[1, 2, 3, 4, 5]
["a", "b", "c", "d"]
[3.4, 2.4, 2.6, 3.5]
Lists can contain values of different data types, so this would be a valid list in Python:
[1, "Emily", 3.4]
We can also assign a list to a variable:
my_list = [1, 2, 3, 4, 5]
letters = ["a", "b", "c", "d"]
🔸 Python Operators
Great. Now you know the syntax of the basic data types and built-in data structures in
Python, so let's start diving into operators in Python. They are essential to perform operations
and to form expressions.
Arithmetic Operators in Python
These operators are:
Addition: +
>>> 5 + 6
11
>>> 0 + 6
6
>>> 3.4 + 5.7
9.1
>>> "Hello" + ", " + "World"
'Hello, World'
>>> True + False
1
💡 Tip: The last two examples are curious, right? This operator behaves differently based on
the data type of the operands.
When they are strings, this operator concatenates the strings and when they are Boolean
values, it performs a particular operation.
In Python, True is equivalent to 1 and False is equivalent to 0. This is why the result is 1 +
0 = 1
Subtraction: -
>>> 5 - 6
-1
>>> 10 - 3
7
>>> 5 - 6
-1
>>> 4.5 - 5.6 - 2.3
-3.3999999999999995
>>> 4.5 - 7
-2.5
>>> - 7.8 - 6.2
-14.0
Multiplication: *
>>> 5 * 6
30
>>> 6 * 7
42
>>> 10 * 100
1000
>>> 4 * 0
0
>>> 3.4 *6.8
23.119999999999997
>>> 4 * (-6)
-24
>>> (-6) * (-8)
48
>>> "Hello" * 4
'HelloHelloHelloHello'
>>> "Hello" * 0
''
>>> "Hello" * -1
''
Nested Lists
Lists can contain values of any data type, even other lists. These inner lists are called nested
lists.
[[1, 2, 3], [4, 5, 6]]
In this example, [1, 2, 3] and [4, 5, 6] are nested lists.
Here we have other valid examples:
[["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
[1, [2, 3, 4], [5, 6, 7], 3.4]
We can access the nested lists using their corresponding index:
>>> my_list = [[1, 2, 3], [4, 5, 6]]
>>> my_list[0]
[1, 2, 3]
>>> my_list[1]
[4, 5, 6]
Nested lists could be used to represent, for example, the structure of a simple 2D game board
where each number could represent a different element or tile:
# Sample Board where:
# 0 = Empty tile
# 1 = Coin
# 2 = Enemy
# 3 = Goal
board = [[0, 0, 1],
[0, 2, 0],
[1, 0, 3]]
List Length
We can use the len() function to get the length of a list (the number of elements it contains).
For example:
>>> my_list = [1, 2, 3, 4]
>>> len(my_list)
4
Update a Value in a List
We can update the value at a particular index with this syntax:
<list_variable>[<index>] = <value>
For example:
>>> letters = ["a", "b", "c", "d"]
>>> letters[0] = "z"
>>> letters
['z', 'b', 'c', 'd']
Add a Value to a List
We can add a new value to the end of a list with the .append() method.
For example:
>>> my_list = [1, 2, 3, 4]
>>> my_list.append(5)
>>> my_list
[1, 2, 3, 4, 5]
Remove a Value from a List
We can remove a value from a list with the .remove() method.
For example:
>>> my_list = [1, 2, 3, 4]
>>> my_list.remove(3)
>>> my_list
[1, 2, 4]
💡 Tip: This will only remove the first occurrence of the element. For example, if we try to
remove the number 3 from a list that has two number 3s, the second number will not be
removed:
>>> my_list = [1, 2, 3, 3, 4]
>>> my_list.remove(3)
>>> my_list
[1, 2, 3, 4]
List Indexing
We can index a list just like we index strings, with indices that start from 0:
>>> letters = ["a", "b", "c", "d"]
>>> letters[0]
'a'
>>> letters[1]
'b'
>>> letters[2]
'c'
>>> letters[3]
'd'
List Slicing
We can also get a slice of a list using the same syntax that we used with strings and we can
omit the parameters to use their default values. Now, instead of adding characters to the slice,
we will be adding the elements of the list.
<list_variable>[start:stop:step]
For example:
>>> my_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
>>> my_list[2:6:2]
['c', 'e']
>>> my_list[2:8]
['c', 'd', 'e', 'f', 'g', 'h']
>>> my_list[1:10]
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> my_list[4:8:2]
['e', 'g']
>>> my_list[::-1]
['i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
>>> my_list[::-2]
['i', 'g', 'e', 'c', 'a']
>>> my_list[8:1:-1]
['i', 'h', 'g', 'f', 'e', 'd', 'c']