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: README.md
+62-40Lines changed: 62 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
2
2
# Introduction to lists
3
3
4
-
So far we have worked with individual pieces of data like the string `hello`, then with variables we saw how to give this data a name. Well, in this lesson, we'll see how we can group data together with lists.
4
+
So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists.
5
5
6
6
### Creating a list
7
7
8
-
A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities to travel to according to Travel and Leisure. We'll see it below, but we must stay focused on Python and data! Here is how we are used to seeing a list of travel locations in a document or on a website.
8
+
A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website.
9
9
10
10
#### Travel Locations
11
11
1. Solta
@@ -21,7 +21,7 @@ A list is our first form of a collection. A collection is just a way of groupin
21
21
11. Toronto
22
22
12. Pyeongchang
23
23
24
-
And here is how that same list looks in Python:
24
+
Here is what that list looks like as a Python`list`:
25
25
26
26
27
27
```python
@@ -46,7 +46,7 @@ And here is how that same list looks in Python:
46
46
47
47
48
48
49
-
So we indicate that we are initializing a `list`by placing a bracket, `[`, and end the list with a closing bracket `']'`. To separate each list item, called an element, we place a comma.
49
+
We indicate that we are initializing a `list`with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma.
50
50
51
51
52
52
```python
@@ -71,7 +71,7 @@ So we indicate that we are initializing a `list` by placing a bracket, `[`, and
71
71
72
72
73
73
74
-
And of course, we can set each list equal to variable so that we can name each list.
74
+
We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list.
Now our `top_travel_cities` list contains multiple elements. And just like we numbered the elements of a list with text:
111
+
Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...
112
112
113
113
1. Solta
114
114
2. Greenville
115
115
3. Buenos Aires
116
116
117
-
A list in Python also assigns a number to each element.
117
+
...a list in Python also assigns a number to each element.
118
118
119
119
120
120
```python
@@ -151,9 +151,9 @@ top_travel_cities[0]
151
151
152
152
153
153
154
-
In the above line we are referencing a list and then using the brackets to access specific elements of our list. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.
154
+
In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.
155
155
156
-
So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`:
156
+
So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`.
157
157
158
158
159
159
```python
@@ -167,7 +167,7 @@ top_travel_cities[2]
167
167
168
168
169
169
170
-
How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the back in move back one.
170
+
How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:
171
171
172
172
173
173
```python
@@ -195,7 +195,7 @@ top_travel_cities[-2]
195
195
196
196
197
197
198
-
Each element in our list is a string, so we can always set an element of our string equal to a variable.
198
+
Each element in our list is a string, so, we can always set an element of our string equal to a variable.
199
199
200
200
201
201
```python
@@ -222,7 +222,7 @@ type(top_canadian_city)
222
222
223
223
224
224
225
-
So now we have a variable of `top_canadian_city`, equal to the string 'toronto', and a variable of `top_travel_cities` equal to the list of cities.
225
+
Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities.
226
226
227
227
228
228
```python
@@ -261,7 +261,7 @@ type(top_travel_cities)
261
261
262
262
### Accessing Multiple Elements
263
263
264
-
Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well.
264
+
Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:
265
265
266
266
267
267
```python
@@ -275,7 +275,7 @@ top_travel_cities[0:2]
275
275
276
276
277
277
278
-
Ok, now to access elements of a list, inside of our brackets we are placing two numbers separated by a colon. The first number indicates the index of the first element we want returned.
278
+
As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned.
279
279
280
280
The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either.
281
281
@@ -302,7 +302,7 @@ top_travel_cities
302
302
303
303
304
304
305
-
So let's try a different experiment to answer our question.
305
+
Let's try a different experiment to answer our question.
306
306
307
307
308
308
```python
@@ -316,7 +316,7 @@ top_travel_cities[4:5]
316
316
317
317
318
318
319
-
Ok, so that second number is not representing the number of elements we want returned. Instead it must be used to indicate the index of the first element not selected.
319
+
Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements.
320
320
321
321
322
322
```python
@@ -330,7 +330,7 @@ top_travel_cities[4:6]
330
330
331
331
332
332
333
-
This operation is called the `slice`. So we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains in tact.
333
+
This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact.
334
334
335
335
336
336
```python
@@ -370,11 +370,11 @@ top_two
370
370
371
371
372
372
373
-
So now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`.
373
+
Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`.
374
374
375
375
### Changing elements with destructive methods
376
376
377
-
Now that we read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method.
377
+
Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method.
378
378
379
379
380
380
```python
@@ -407,15 +407,35 @@ top_travel_cities
407
407
408
408
409
409
410
-
You will see 'San Antonio' included in the list. So note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list.
410
+
You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list.
411
411
412
412
413
413
```python
414
414
top_travel_cities.append('San Antonio')
415
415
top_travel_cities
416
416
```
417
417
418
-
The `pop` method is available to call on any list, and removes the last element from the list. As you can see below, calling `pop` removed our last element.
418
+
419
+
420
+
421
+
['Solta',
422
+
'Greenville',
423
+
'Buenos Aires',
424
+
'Los Cabos',
425
+
'Walla Walla Valley',
426
+
'Marakesh',
427
+
'Albuquerque',
428
+
'Archipelago Sea',
429
+
'Iguazu Falls',
430
+
'Salina Island',
431
+
'Toronto',
432
+
'Pyeongchang',
433
+
'San Antonio',
434
+
'San Antonio']
435
+
436
+
437
+
438
+
If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element.
419
439
420
440
421
441
```python
@@ -429,7 +449,7 @@ top_travel_cities.pop()
429
449
430
450
431
451
432
-
Now if we want to change an element from the middle of the list, we can access and then reassign that element. So for example, let's change 'Walla Walla Valley' to the number 4.
452
+
Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4.
433
453
434
454
435
455
```python
@@ -472,14 +492,14 @@ top_travel_cities
472
492
473
493
474
494
475
-
And our list is now changed. It's not a sensible list right now, so let's change it back.
495
+
Our list is changed, but now it's not as sensible, so let's change it back.
476
496
477
497
478
498
```python
479
499
top_travel_cities[4] ='Walla Walla Valley'
480
500
```
481
501
482
-
And our list is alright.
502
+
With that, our list is back to the way we like it.
483
503
484
504
485
505
```python
@@ -507,7 +527,7 @@ top_travel_cities
507
527
508
528
### Finding Unique elements and length of lists
509
529
510
-
If we are not sure if there are repeated elements, we can use Python to get a unique list.
530
+
If we are not sure whether there are repeated elements, we can use Python to get a unique list.
511
531
512
532
513
533
```python
@@ -547,13 +567,13 @@ The set function initializes a new set in Python. A set is a different type col
> **Note:***For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*
710
+
689
711
### Summary
690
712
691
-
In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve. And our indices began at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them.
713
+
In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them.
0 commit comments