Lists ¶
Lists represent the simplest, most versatile, and most used ordered collections type.
They can story more than one type of data, they have a specific order, and the contents can change.
For example, we can create a list of things I have in my pocket.
We instruct Python to create a list by surrounding data with square brackets:
[ ]
.
in_my_pocket = [21, False, None, "apple", 3.14]
print(in_my_pocket)
[21, False, None, 'apple', 3.14]
We do not necessarily have to start with data inside.
We can create an empty list as well with just
[ ]
.
in_my_other_pocket = []
print(in_my_other_pocket)
[]
Indexing ¶
One of the most fundamental things you use lists for is to get individual values.
Because the order of the items in the list matter, I can refer to an item using it's position in the list.
This position in programming languages is called the
index
; with the first index being
0
, then
1
, then
2
, and so on.
Let's print the first value.
print(in_my_pocket[0])
21
You may have noticed that we are using
[]
to both create the list and index it.
This was designed on purpose to be more readable, and the main difference is if
[]
is directly next to a variable like in our
in_my_pocket[0]
example.
If we did
in_my_pocket [0]
we would get an error.
While we can count from start to end of the list, we can also count backwards.
To get the last element of a list, we can use the index
-1
.
print(in_my_pocket[-1])
3.14
A table of the available elements are shown below.
Element | Index from start | Index from end |
---|---|---|
21
|
0 | -5 |
False
|
1 | -4 |
None
|
2 | -3 |
"apple"
|
3 | -2 |
3.14
|
4 | -1 |
Either of these indices would get you the same element.
Length ¶
Another important thing is that you cannot use an index that is outside the bounds of the list.
For example we cannot ask for the 6th element of
in_my_pocket
since there are only five items.
We can get the length of a list by using the
len
function.
print(len(in_my_pocket))
5
This means that there are 5 elements and the largest (positive) index I can use is minus one of that, so
4
.
Slicing ¶
While accessing individual list elements is useful, often we want to extract a subsection, or slice, of a list's items. This is accomplished in Python using slicing. Slicing allows copying a portion of a list—either from one or both sides. This enables cleanly breaking large lists into usable parts.
In Python, the colon
:
is used for slicing with the general syntax of
start:stop:step
.
-
start
: The index at which the slice begins (inclusive). If omitted orNone
, it defaults to the beginning of the sequence. -
stop
: The index at which the slice ends (exclusive). If omitted orNone
, it defaults to the end of the sequence. -
step
: The step size or the number of indices between each slice. If omitted orNone
, it defaults to 1.
First, let us print the full list like we normally have been doing.
print(in_my_pocket)
[21, False, None, 'apple', 3.14]
We can get the same view if we slice
in_my_pocket
without specifying
start
,
stop
, or
step
.
print(in_my_pocket[::])
[21, False, None, 'apple', 3.14]
Remember that
None
is often used to specify the absence of a value, so it should give us the same as
[::]
.
print(in_my_pocket[None:None:None])
[21, False, None, 'apple', 3.14]
Yup!
This gives us the same slice of the list
in_my_pocket
.
Now, if I want only the first two elements of the list, I want to tell Python to stop slicing (and not include) at the index of
2
.
print(in_my_pocket[:2])
[21, False]
I can also tell Python I want the elements at index
2
and
3
.
print(in_my_pocket[2:4])
[None, 'apple']
in_my_pocket = [21, False, None, "apple", 3.14]
print(in_my_pocket)
in_my_pocket[2] = "wallet"
print(in_my_pocket)
[21, False, None, 'apple', 3.14] [21, False, 'wallet', 'apple', 3.14]
This works the same way as memory allocation for
variables
.
Python will go to where the third element is located in memory, throw out
None
, and put
"wallet"
there instead.
Append ¶
However, what if I want to add an element to my list and not remove anything?
We can do this with
append
.
For example, let's say I keep everything in my pocket that is already there, but then I want to add my keys.
print(in_my_pocket)
print(len(in_my_pocket))
in_my_pocket.append("keys")
print(in_my_pocket)
print(len(in_my_pocket))
[21, False, 'wallet', 'apple', 3.14] 5 [21, False, 'wallet', 'apple', 3.14, 'keys'] 6
Now our list has an additional element and a new length of
6
.
Delete ¶
Just like we can add things to a list, we can also remove them with the
del
keyword in Python
print(in_my_pocket)
print(len(in_my_pocket))
del in_my_pocket[3]
print(in_my_pocket)
print(len(in_my_pocket))
[21, False, 'wallet', 'apple', 3.14, 'keys'] 6 [21, False, 'wallet', 3.14, 'keys'] 5