1.9 KiB
1.9 KiB
id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
| id | source_exercise_id | title | section | source_path | source_repo | source_commit | student_visible_solution | has_private_solution | skip |
|---|---|---|---|---|---|---|---|---|---|
| practical-python-1.22 | 1.22 | Appending, inserting, and deleting items | 1.5 Lists | 01_Introduction/05_Lists.md | https://github.com/dabeaz-course/practical-python | 93dca856b41c61a0a0f85ae334116e4c125629ea | false | false | false |
Exercise 1.22: Appending, inserting, and deleting items
Source: Practical Python Programming,
01_Introduction/05_Lists.md.
Exercise 1.22: Appending, inserting, and deleting items
Use the append() method to add the symbol 'RHT' to end of symlist.
>>> # append 'RHT'
>>> symlist
['HPQ', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG', 'RHT']
>>>
Use the insert() method to insert the symbol 'AA' as the second item in the list.
>>> # Insert 'AA' as the second item in the list
>>> symlist
['HPQ', 'AA', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG', 'RHT']
>>>
Use the remove() method to remove 'MSFT' from the list.
>>> # Remove 'MSFT'
>>> symlist
['HPQ', 'AA', 'AAPL', 'AIG', 'YHOO', 'GOOG', 'RHT']
>>>
Append a duplicate entry for 'YHOO' at the end of the list.
Note: it is perfectly fine for a list to have duplicate values.
>>> # Append 'YHOO'
>>> symlist
['HPQ', 'AA', 'AAPL', 'AIG', 'YHOO', 'GOOG', 'RHT', 'YHOO']
>>>
Use the index() method to find the first position of 'YHOO' in the list.
>>> # Find the first index of 'YHOO'
4
>>> symlist[4]
'YHOO'
>>>
Count how many times 'YHOO' is in the list:
>>> symlist.count('YHOO')
2
>>>
Remove the first occurrence of 'YHOO'.
>>> # Remove first occurrence 'YHOO'
>>> symlist
['HPQ', 'AA', 'AAPL', 'AIG', 'GOOG', 'RHT', 'YHOO']
>>>
Just so you know, there is no method to find or remove all occurrences of an item. However, we'll see an elegant way to do this in section 2.