1
+ [ Contents] ( ../Contents ) \| [ Previous (1.4 Strings)] ( 04_Strings ) \| [ Next (1.6 Files)] ( 06_Files )
2
+
1
3
# 1.5 Lists
2
4
5
+ This section introduces lists, Python's primary type for holding an ordered collection of values.
6
+
3
7
### Creating a List
4
8
5
- Use square brackets to define a list:
9
+ Use square brackets to define a list literal :
6
10
7
11
``` python
8
12
names = [ ' Elwood' , ' Jake' , ' Curtis' ]
@@ -12,7 +16,7 @@ nums = [ 39, 38, 42, 65, 111]
12
16
Sometimes lists are created by other methods. For example, a string can be split into a
13
17
list using the ` split() ` method:
14
18
15
- ``` pycon
19
+ ``` python
16
20
>> > line = ' GOOG,100,490.10'
17
21
>> > row = line.split(' ,' )
18
22
>> > row
@@ -53,7 +57,7 @@ Negative indices count from the end.
53
57
names[- 1 ] # 'Curtis'
54
58
```
55
59
56
- You can change any item in the list.
60
+ You can change any item in a list.
57
61
58
62
``` python
59
63
names[1 ] = ' Joliet Jake'
@@ -83,7 +87,7 @@ s * 3 # [1, 2, 3, 1, 2, 3, 1, 2, 3]
83
87
84
88
### List Iteration and Search
85
89
86
- Iterating over the list contents.
90
+ Use ` for ` to iterate over the list contents.
87
91
88
92
``` python
89
93
for name in names:
@@ -117,8 +121,9 @@ names.remove('Curtis')
117
121
del names[1 ]
118
122
```
119
123
120
- Removing an item does not create a hole. Other items will move down to fill the space vacated.
121
- If there are more than one occurrence of the element, ` .remove() ` will remove only the first occurrence.
124
+ Removing an item does not create a hole. Other items will move down
125
+ to fill the space vacated. If there are more than one occurrence of
126
+ the element, ` remove() ` will remove only the first occurrence.
122
127
123
128
### List Sorting
124
129
@@ -137,33 +142,39 @@ s = ['foo', 'bar', 'spam']
137
142
s.sort() # ['bar', 'foo', 'spam']
138
143
```
139
144
145
+ Use ` sorted() ` if you'd like to make a new list instead:
146
+
147
+ ``` python
148
+ t = sorted (s) # s unchanged, t holds sorted values
149
+ ```
150
+
140
151
### Lists and Math
141
152
142
153
* Caution: Lists were not designed for math operations.*
143
154
144
- ``` pycon
155
+ ``` python
145
156
>> > nums = [1 , 2 , 3 , 4 , 5 ]
146
157
>> > nums * 2
147
158
[1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 , 4 , 5 ]
148
159
>> > nums + [10 , 11 , 12 , 13 , 14 ]
149
160
[1 , 2 , 3 , 4 , 5 , 10 , 11 , 12 , 13 , 14 ] >> >
150
161
```
151
162
152
- Specifically, lists don't represent vectors/matrices as in MATLAB, Octave, IDL , etc.
163
+ Specifically, lists don't represent vectors/matrices as in MATLAB, Octave, R , etc.
153
164
However, there are some packages to help you with that (e.g. [ numpy] ( https://numpy.org ) ).
154
165
155
166
## Exercises
156
167
157
168
In this exercise, we experiment with Python's list datatype. In the last section,
158
169
you worked with strings containing stock symbols.
159
170
160
- ``` pycon
171
+ ``` python
161
172
>> > symbols = ' HPQ,AAPL,IBM,MSFT,YHOO,DOA,GOOG'
162
173
```
163
174
164
175
Split it into a list of names using the ` split() ` operation of strings:
165
176
166
- ``` pycon
177
+ ``` python
167
178
>> > symlist = symbols.split(' ,' )
168
179
```
169
180
@@ -228,7 +239,7 @@ For instance, in the above example, the last two items of `symlist` got replaced
228
239
The ` for ` loop works by looping over data in a sequence such as a list.
229
240
Check this out by typing the following loop and watching what happens:
230
241
231
- ``` pycon
242
+ ``` python
232
243
>> > for s in symlist:
233
244
print (' s =' , s)
234
245
# Look at the output
@@ -238,7 +249,7 @@ Check this out by typing the following loop and watching what happens:
238
249
239
250
Use the ` in ` or ` not in ` operator to check if ` 'AIG' ` ,` 'AA' ` , and ` 'CAT' ` are in the list of symbols.
240
251
241
- ``` pycon
252
+ ``` python
242
253
>> > # Is 'AIG' IN the `symlist`?
243
254
True
244
255
>> > # Is 'AA' IN the `symlist`?
252
263
253
264
Use the ` append() ` method to add the symbol ` 'RHT' ` to end of ` symlist ` .
254
265
255
- ``` pycon
266
+ ``` python
256
267
>> > # append 'RHT'
257
268
>> > symlist
258
269
[' HPQ' , ' AAPL' , ' AIG' , ' MSFT' , ' YHOO' , ' GOOG' , ' RHT' ]
@@ -261,7 +272,7 @@ Use the `append()` method to add the symbol `'RHT'` to end of `symlist`.
261
272
262
273
Use the ` insert() ` method to insert the symbol ` 'AA' ` as the second item in the list.
263
274
264
- ``` pycon
275
+ ``` python
265
276
>> > # Insert 'AA' as the second item in the list
266
277
>> > symlist
267
278
[' HPQ' , ' AA' , ' AAPL' , ' AIG' , ' MSFT' , ' YHOO' , ' GOOG' , ' RHT' ]
@@ -270,7 +281,7 @@ Use the `insert()` method to insert the symbol `'AA'` as the second item in the
270
281
271
282
Use the ` remove() ` method to remove ` 'MSFT' ` from the list.
272
283
273
- ``` pycon
284
+ ``` python
274
285
>> > # Remove 'MSFT'
275
286
>> > symlist
276
287
[' HPQ' , ' AA' , ' AAPL' , ' AIG' , ' YHOO' , ' GOOG' , ' RHT' ]
@@ -281,7 +292,7 @@ Append a duplicate entry for `'YHOO'` at the end of the list.
281
292
282
293
* Note: it is perfectly fine for a list to have duplicate values.*
283
294
284
- ``` pycon
295
+ ``` python
285
296
>> > # Append 'YHOO'
286
297
>> > symlist
287
298
[' HPQ' , ' AA' , ' AAPL' , ' AIG' , ' YHOO' , ' GOOG' , ' RHT' , ' YHOO' ]
@@ -290,7 +301,7 @@ Append a duplicate entry for `'YHOO'` at the end of the list.
290
301
291
302
Use the ` index() ` method to find the first position of ` 'YHOO' ` in the list.
292
303
293
- ``` pycon
304
+ ``` python
294
305
>> > # Find the first index of 'YHOO'
295
306
4
296
307
>> > symlist[4 ]
@@ -300,15 +311,15 @@ Use the `index()` method to find the first position of `'YHOO'` in the list.
300
311
301
312
Count how many times ` 'YHOO' ` is in the list:
302
313
303
- ``` pycon
314
+ ``` python
304
315
>> > symlist.count(' YHOO' )
305
316
2
306
317
>> >
307
318
```
308
319
309
320
Remove the first occurrence of ` 'YHOO' ` .
310
321
311
- ``` pycon
322
+ ``` python
312
323
>> > # Remove first occurrence 'YHOO'
313
324
>> > symlist
314
325
[' HPQ' , ' AA' , ' AAPL' , ' AIG' , ' GOOG' , ' RHT' , ' YHOO' ]
@@ -322,7 +333,7 @@ However, we'll see an elegant way to do this in section 2.
322
333
323
334
Want to sort a list? Use the ` sort() ` method. Try it out:
324
335
325
- ``` pycon
336
+ ``` python
326
337
>> > symlist.sort()
327
338
>> > symlist
328
339
[' AA' , ' AAPL' , ' AIG' , ' GOOG' , ' HPQ' , ' RHT' , ' YHOO' ]
@@ -331,7 +342,7 @@ Want to sort a list? Use the `sort()` method. Try it out:
331
342
332
343
Want to sort in reverse? Try this:
333
344
334
- ``` pycon
345
+ ``` python
335
346
>> > symlist.sort(reverse = True )
336
347
>> > symlist
337
348
[' YHOO' , ' RHT' , ' HPQ' , ' GOOG' , ' AIG' , ' AAPL' , ' AA' ]
@@ -345,7 +356,7 @@ Note: Sorting a list modifies its contents 'in-place'. That is, the elements of
345
356
Want to take a list of strings and join them together into one string?
346
357
Use the ` join() ` method of strings like this (note: this looks funny at first).
347
358
348
- ``` pycon
359
+ ``` python
349
360
>> > a = ' ,' .join(symlist)
350
361
>> > a
351
362
' YHOO,RHT,HPQ,GOOG,AIG,AAPL,AA'
@@ -363,7 +374,7 @@ Use the `join()` method of strings like this (note: this looks funny at first).
363
374
Lists can contain any kind of object, including other lists (e.g., nested lists).
364
375
Try this out:
365
376
366
- ``` pycon
377
+ ``` python
367
378
>> > nums = [101 , 102 , 103 ]
368
379
>> > items = [' spam' , symlist, nums]
369
380
>> > items
@@ -375,7 +386,7 @@ The first element is a string, but the other two elements are lists.
375
386
376
387
You can access items in the nested lists by using multiple indexing operations.
377
388
378
- ``` pycon
389
+ ``` python
379
390
>> > items[0 ]
380
391
' spam'
381
392
>> > items[0 ][0 ]
0 commit comments