91 lines
1.9 KiB
Markdown
91 lines
1.9 KiB
Markdown
---
|
|
id: practical-python-1.22
|
|
source_exercise_id: "1.22"
|
|
title: "Appending, inserting, and deleting items"
|
|
section: "1.5 Lists"
|
|
source_path: "01_Introduction/05_Lists.md"
|
|
source_repo: "https://github.com/dabeaz-course/practical-python"
|
|
source_commit: "93dca856b41c61a0a0f85ae334116e4c125629ea"
|
|
student_visible_solution: false
|
|
has_private_solution: false
|
|
skip: 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`.
|
|
|
|
```python
|
|
>>> # 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.
|
|
|
|
```python
|
|
>>> # 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.
|
|
|
|
```python
|
|
>>> # 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.*
|
|
|
|
```python
|
|
>>> # 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.
|
|
|
|
```python
|
|
>>> # Find the first index of 'YHOO'
|
|
4
|
|
>>> symlist[4]
|
|
'YHOO'
|
|
>>>
|
|
```
|
|
|
|
Count how many times `'YHOO'` is in the list:
|
|
|
|
```python
|
|
>>> symlist.count('YHOO')
|
|
2
|
|
>>>
|
|
```
|
|
|
|
Remove the first occurrence of `'YHOO'`.
|
|
|
|
```python
|
|
>>> # 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.
|
|
|
|
## 关联来源
|
|
|
|
- [[summaries/05_Lists]]
|