1+ [ Contents] ( ../Contents ) \| [ Previous (1.4 Strings)] ( 04_Strings ) \| [ Next (1.6 Files)] ( 06_Files )
2+
13# 1.5 Lists
24
5+ This section introduces lists, Python's primary type for holding an ordered collection of values.
6+
37### Creating a List
48
5- Use square brackets to define a list:
9+ Use square brackets to define a list literal :
610
711``` python
812names = [ ' Elwood' , ' Jake' , ' Curtis' ]
@@ -12,7 +16,7 @@ nums = [ 39, 38, 42, 65, 111]
1216Sometimes lists are created by other methods. For example, a string can be split into a
1317list using the ` split() ` method:
1418
15- ``` pycon
19+ ``` python
1620>> > line = ' GOOG,100,490.10'
1721>> > row = line.split(' ,' )
1822>> > row
@@ -53,7 +57,7 @@ Negative indices count from the end.
5357names[- 1 ] # 'Curtis'
5458```
5559
56- You can change any item in the list.
60+ You can change any item in a list.
5761
5862``` python
5963names[1 ] = ' Joliet Jake'
@@ -83,7 +87,7 @@ s * 3 # [1, 2, 3, 1, 2, 3, 1, 2, 3]
8387
8488### List Iteration and Search
8589
86- Iterating over the list contents.
90+ Use ` for ` to iterate over the list contents.
8791
8892``` python
8993for name in names:
@@ -117,8 +121,9 @@ names.remove('Curtis')
117121del names[1 ]
118122```
119123
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.
122127
123128### List Sorting
124129
@@ -137,33 +142,39 @@ s = ['foo', 'bar', 'spam']
137142s.sort() # ['bar', 'foo', 'spam']
138143```
139144
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+
140151### Lists and Math
141152
142153* Caution: Lists were not designed for math operations.*
143154
144- ``` pycon
155+ ``` python
145156>> > nums = [1 , 2 , 3 , 4 , 5 ]
146157>> > nums * 2
147158[1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 , 4 , 5 ]
148159>> > nums + [10 , 11 , 12 , 13 , 14 ]
149160[1 , 2 , 3 , 4 , 5 , 10 , 11 , 12 , 13 , 14 ] >> >
150161```
151162
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.
153164However, there are some packages to help you with that (e.g. [ numpy] ( https://numpy.org ) ).
154165
155166## Exercises
156167
157168In this exercise, we experiment with Python's list datatype. In the last section,
158169you worked with strings containing stock symbols.
159170
160- ``` pycon
171+ ``` python
161172>> > symbols = ' HPQ,AAPL,IBM,MSFT,YHOO,DOA,GOOG'
162173```
163174
164175Split it into a list of names using the ` split() ` operation of strings:
165176
166- ``` pycon
177+ ``` python
167178>> > symlist = symbols.split(' ,' )
168179```
169180
@@ -228,7 +239,7 @@ For instance, in the above example, the last two items of `symlist` got replaced
228239The ` for ` loop works by looping over data in a sequence such as a list.
229240Check this out by typing the following loop and watching what happens:
230241
231- ``` pycon
242+ ``` python
232243>> > for s in symlist:
233244 print (' s =' , s)
234245# Look at the output
@@ -238,7 +249,7 @@ Check this out by typing the following loop and watching what happens:
238249
239250Use the ` in ` or ` not in ` operator to check if ` 'AIG' ` ,` 'AA' ` , and ` 'CAT' ` are in the list of symbols.
240251
241- ``` pycon
252+ ``` python
242253>> > # Is 'AIG' IN the `symlist`?
243254True
244255>> > # Is 'AA' IN the `symlist`?
252263
253264Use the ` append() ` method to add the symbol ` 'RHT' ` to end of ` symlist ` .
254265
255- ``` pycon
266+ ``` python
256267>> > # append 'RHT'
257268>> > symlist
258269[' HPQ' , ' AAPL' , ' AIG' , ' MSFT' , ' YHOO' , ' GOOG' , ' RHT' ]
@@ -261,7 +272,7 @@ Use the `append()` method to add the symbol `'RHT'` to end of `symlist`.
261272
262273Use the ` insert() ` method to insert the symbol ` 'AA' ` as the second item in the list.
263274
264- ``` pycon
275+ ``` python
265276>> > # Insert 'AA' as the second item in the list
266277>> > symlist
267278[' 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
270281
271282Use the ` remove() ` method to remove ` 'MSFT' ` from the list.
272283
273- ``` pycon
284+ ``` python
274285>> > # Remove 'MSFT'
275286>> > symlist
276287[' HPQ' , ' AA' , ' AAPL' , ' AIG' , ' YHOO' , ' GOOG' , ' RHT' ]
@@ -281,7 +292,7 @@ Append a duplicate entry for `'YHOO'` at the end of the list.
281292
282293* Note: it is perfectly fine for a list to have duplicate values.*
283294
284- ``` pycon
295+ ``` python
285296>> > # Append 'YHOO'
286297>> > symlist
287298[' HPQ' , ' AA' , ' AAPL' , ' AIG' , ' YHOO' , ' GOOG' , ' RHT' , ' YHOO' ]
@@ -290,7 +301,7 @@ Append a duplicate entry for `'YHOO'` at the end of the list.
290301
291302Use the ` index() ` method to find the first position of ` 'YHOO' ` in the list.
292303
293- ``` pycon
304+ ``` python
294305>> > # Find the first index of 'YHOO'
2953064
296307>> > symlist[4 ]
@@ -300,15 +311,15 @@ Use the `index()` method to find the first position of `'YHOO'` in the list.
300311
301312Count how many times ` 'YHOO' ` is in the list:
302313
303- ``` pycon
314+ ``` python
304315>> > symlist.count(' YHOO' )
3053162
306317>> >
307318```
308319
309320Remove the first occurrence of ` 'YHOO' ` .
310321
311- ``` pycon
322+ ``` python
312323>> > # Remove first occurrence 'YHOO'
313324>> > symlist
314325[' HPQ' , ' AA' , ' AAPL' , ' AIG' , ' GOOG' , ' RHT' , ' YHOO' ]
@@ -322,7 +333,7 @@ However, we'll see an elegant way to do this in section 2.
322333
323334Want to sort a list? Use the ` sort() ` method. Try it out:
324335
325- ``` pycon
336+ ``` python
326337>> > symlist.sort()
327338>> > symlist
328339[' AA' , ' AAPL' , ' AIG' , ' GOOG' , ' HPQ' , ' RHT' , ' YHOO' ]
@@ -331,7 +342,7 @@ Want to sort a list? Use the `sort()` method. Try it out:
331342
332343Want to sort in reverse? Try this:
333344
334- ``` pycon
345+ ``` python
335346>> > symlist.sort(reverse = True )
336347>> > symlist
337348[' 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
345356Want to take a list of strings and join them together into one string?
346357Use the ` join() ` method of strings like this (note: this looks funny at first).
347358
348- ``` pycon
359+ ``` python
349360>> > a = ' ,' .join(symlist)
350361>> > a
351362' 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).
363374Lists can contain any kind of object, including other lists (e.g., nested lists).
364375Try this out:
365376
366- ``` pycon
377+ ``` python
367378>> > nums = [101 , 102 , 103 ]
368379>> > items = [' spam' , symlist, nums]
369380>> > items
@@ -375,7 +386,7 @@ The first element is a string, but the other two elements are lists.
375386
376387You can access items in the nested lists by using multiple indexing operations.
377388
378- ``` pycon
389+ ``` python
379390>> > items[0 ]
380391' spam'
381392>> > items[0 ][0 ]
0 commit comments