-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.qmd
More file actions
230 lines (136 loc) · 5.16 KB
/
lists.qmd
File metadata and controls
230 lines (136 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
---
filters:
- pyodide
---
# Lists
{{< video https://youtu.be/8DlOv18BoqM >}}
Lists are extremely useful in Python, allowing us to store multiple items of ordered data to which we can refer later.
We can create an empty list if we want - to do this we just use two square brackets with nothing in between.
```{pyodide-python}
# Define a new empty list
my_empty_list = []
```
However, you'll often want to create a list with some elements already in it.
```{pyodide-python}
# Define a new list with some starting elements
my_list = [3, 4, 7, "hello"]
print(my_list)
```
We can also add an extra item to a list.
```{pyodide-python}
# Define a new list with some starting elements
my_list = [3, 4, 7, "hello"]
print(f"Here is our original list: {my_list}")
my_list.append(2)
print(f"Here is our updated list: {my_list}")
```
Note that we didn't **assign** the new list back to the variable `my_list`.
i.e. we didn't write
```
my_list = my_list.append(2)
```
This is because append updates the object without needing to be assigned!
:::{.callout-info}
Why is append written after my_list instead of before?
This is because it's a *method* of the list *class*.
The dot here is known as dot notation. We’ll cover this more in Object Oriented Programming, but basically a dot is a way of getting to the methods (functions) and attributes (variables) stored in an object (a list object here).
So this line says “Use the append **method** of a **list object**”
:::
## Indices
{{< video https://youtu.be/8JlVrOqvm4M >}}
Lists are ordered, which means that we can refer to elements in a list by their location - their index.
To refer to an element in a list by its index, we use the following notation :
```{python}
#| eval: false
my_list[x]
```
where x is the index number of the element we want to reference.
HOWEVER, Python (like many languages) starts counting from 0. Which means the first element of a list has an index of 0, the second an index of 1 etc. This will catch you out if you’re new to coding, especially as a few constructs in Python start counting from 1…
So, to reference the fourth element in a list my_list, we would use :
```{python}
#| eval: false
my_list[3]
```
Let's run this below!
Try changing the index (the '3') in the line `print(my_list[3])`
```{pyodide-python}
# Define a new list with some starting elements
my_list = [3, 4, 7, "hello"]
print(my_list[3])
```
## Negative Indexing and Slicing
{{< video https://youtu.be/Snzu2JeHioo >}}
We can also use negative indexing to refer to an item based on its position from the end of the list. Here, -1 would refer to the last element, -2 to the penultimate element etc.
```{pyodide-python}
# Define a new list with some starting elements
my_list = [3, 4, 7, "hello"]
print(my_list[-2])
```
If we want to refer to multiple items in a list, we can use a slice. Here, we use a colon to denote the indices of the start and end elements that we want.
:::{.callout-tip}
The start element is included.
The end element is not included.
:::
```{pyodide-python}
# Define a new list with some starting elements
my_list = [3, 4, 7, "hello"]
print(my_list[1:3])
print(my_list[:2])
print(my_list[3:])
```
## Length and Removal
To find the length of a list (the number of elements it contains), we simply use the len() function :
```{pyodide-python}
# Define a new list with some starting elements
my_list = [3, 4, 7, "hello"]
print(len(my_list))
```
We can remove an item from a list by specifying the element we want to remove, and using remove :
```{pyodide-python}
my_list = [3, 4, 7, "hello"]
print(f"Here is our original list: {my_list}")
my_list.remove("hello")
print(f"Here is our updated list: {my_list}")
```
Or by giving the index of the element we want to remove, and using pop:
```{pyodide-python}
my_list = [3, 4, 7, "hello"]
print(f"Here is our original list: {my_list}")
my_list.pop(0)
print(f"Here is our updated list: {my_list}")
```
If we want to remove the last element from a list, we can just use pop without specifying an index :
```{pyodide-python}
my_list = [3, 4, 7, "hello"]
print(f"Here is our original list: {my_list}")
my_list.pop()
print(f"Here is our updated list: {my_list}")
```
## Checking Existence
We can check whether or not an element exists in a list using conditional logic combined with the in keyword :
```{pyodide-python}
my_list = [3, 4, 7, "hello"]
if 2 in my_list:
print("Already there")
```
Or whether something is not in a list by additionally using the not keyword :
```{pyodide-python}
my_list = [3, 4, 7, "hello"]
if 5 not in my_list:
print("It's not there!")
```
## Copying lists
If we want to copy a list, we might be tempted to write this :
```{python}
#| eval: false
my_list = [3, 4, 7, "hello", 2]
copy_of_my_list = my_list
```
The previous example will create a second reference to the same list. In other words, there is still one list, just two different names for it.
To create a copy, we need to use the copy() function of a list object :
```{python}
#| eval: false
my_list = [3, 4, 7, "hello", 2]
copy_of_my_list = my_list.copy()
```
Now we have two lists, and can work with them independently (ie changes made to one will not affect the other).