You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: curriculum/challenges/english/25-front-end-development/lecture-working-with-loops-and-sequences/67fe8399c41a212871025f96.md
+170-9Lines changed: 170 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,175 @@
1
1
---
2
2
id: 67fe8399c41a212871025f96
3
3
title: What Are Lists and How Do They Work?
4
-
challengeType: 11
5
-
videoId: nVAaxZ34khk
4
+
challengeType: 19
6
5
dashedName: what-are-lists-and-how-do-they-work
7
6
---
8
7
9
8
# --description--
10
9
11
-
Watch the video or read the transcript and answer the questions below.
10
+
Over the next few lectures we are going to learn about lists, tuples, and ranges, which are three basic sequence types used in Python.
11
+
12
+
The list data type is an ordered sequence of elements that can be comprised of strings, numbers, or even other lists. Similar to JavaScript, lists are mutable and use zero-based indexing, meaning that the first element of the list is at index zero.
13
+
14
+
Here is the basic syntax for a list:
15
+
16
+
```py
17
+
cities = ['Los Angeles', 'London', 'Tokyo']
18
+
```
19
+
20
+
To access an element from the `cities` list, you can reference its index number in the sequence. Here is an example of accessing the first element of the `cities` list:
21
+
22
+
```py
23
+
cities = ['Los Angeles', 'London', 'Tokyo']
24
+
cities[0] # 'Los Angeles'
25
+
```
26
+
27
+
Negative indexing is used to access elements starting from the end of the list instead of the beginning at index `0`. To access the last element of any list, you can use `-1` like this:
28
+
29
+
```py
30
+
cities = ['Los Angeles', 'London', 'Tokyo']
31
+
cities[-1] # 'Tokyo'
32
+
```
33
+
34
+
Another way to create a list is to use the `list()` constructor. The `list()` constructor is used to convert an iterable into a list like this:
Since lists are mutable, you can update any element in the list as long as you pass in a valid index number. If you pass in an index (either positive or negative) that is out of bounds for the list, then you will receive an `IndexError`:
In this example, we have one nested list containing three popular programming languages. To access the nested list, you will need to access it using index `2` since lists are zero based indexed:
Another common technique used with lists is unpacking values.
109
+
110
+
Unpacking values from a list is a technique used to assign values from a list to new variables. Here is an example of unpacking a `developer` list into new variables called `name`, `age` and `job`.
111
+
112
+
```py
113
+
developer = ['Alice', 34, 'Rust Developer']
114
+
name, age, job = developer
115
+
116
+
print(name) # 'Alice'
117
+
print(age) # 34
118
+
print(job) # 'Rust Developer'
119
+
```
120
+
121
+
Here, `name` has the value `'Alice'`, `age` has the value `34`, and `job` has the value `'Rust Developer'`.
122
+
123
+
If you need to collect any remaining elements from a list, you can use the asterisk (`*`) operator like this:
124
+
125
+
```py
126
+
developer = ['Alice', 34, 'Rust Developer']
127
+
name, *rest = developer
128
+
129
+
print(name) # 'Alice'
130
+
print(rest) # [34, 'Rust Developer']
131
+
```
132
+
133
+
In this example, `name` will still have the value `'Alice'`, and `rest` is a list of two items: the number `34` and the string `'Rust Developer'`.
134
+
135
+
If the numbers of variables on the left side of the assignment operator doesn't match the total numbers of items in the list, then you will receive a `ValueError`:
136
+
137
+
```py
138
+
developer = ['Alice', 34, 'Rust Developer']
139
+
name, age, job, city = developer
140
+
141
+
"""
142
+
Traceback (most recent call last):
143
+
File "<stdin>", line 2, in <module>
144
+
ValueError: not enough values to unpack (expected 4, got 3)
145
+
"""
146
+
```
147
+
148
+
The last concept we will look at is the slice operator (`:`). Similar to strings, you can access portions of a list by using the slice operator like this:
In this example, the start index is `1` since that points to the second item in the list. Then we use the slice operator followed by an end index of `4`, which includes everything up to (but not including), the item at that index.
156
+
157
+
Another thing you can do with the slice operator `:` is specify a step interval which determines how much to increment between the indices. Let's say you had a list of numbers like this:
158
+
159
+
```py
160
+
numbers = [1, 2, 3, 4, 5, 6]
161
+
```
162
+
163
+
If you wanted to extract a list of just even numbers, you can use the slicing operator like this:
164
+
165
+
```py
166
+
numbers = [1, 2, 3, 4, 5, 6]
167
+
numbers[1::2] # [2, 4, 6]
168
+
```
169
+
170
+
The first even number is at index `1`, so that will be the start index. Since we want to go through the end of the list, then we omit the end index. Lastly, we specify `2` for the optional step interval so it will only increment by `2` instead of the default `1`.
171
+
172
+
Lists are a useful and flexible data structure that you will use a lot in your Python programs. In the next lecture, you will learn about common methods that you can use with lists.
12
173
13
174
# --questions--
14
175
@@ -22,7 +183,7 @@ They are two-based indexed.
22
183
23
184
### --feedback--
24
185
25
-
Refer to the beginning of the video for the answer.
186
+
Refer to the beginning of the lecture for the answer.
26
187
27
188
---
28
189
@@ -34,15 +195,15 @@ They are only used inside of classes.
34
195
35
196
### --feedback--
36
197
37
-
Refer to the beginning of the video for the answer.
198
+
Refer to the beginning of the lecture for the answer.
38
199
39
200
---
40
201
41
202
They are rarely used in Python programs.
42
203
43
204
### --feedback--
44
205
45
-
Refer to the beginning of the video for the answer.
206
+
Refer to the beginning of the lecture for the answer.
0 commit comments