### 3.2增加
---
作用:增加指定数据到列表中
- append():列表追加数据
1. 语法
```python
列表序列.append(数据)
```
2. 体验
```python
name_list = ['Tom','Lily','Rose']
name_list.append('xiaoming')
# 结果 :['Tom','Lily','Rose','xiaoming']
print(name_list)
```
- 列表增加数据的时候,直接在原列表中追加了指定数据,即修改了原列表,故列表为可变类型是数据
3. 注意点
如果append()追加的数据是一个序列,则追加整个序列到列表
```python
name_list = ['Tom','Lily','Rose']
name_list.append(['xiaoming','xiaohong'])
# 结果: ['Tom','Lily','Rose',['xiaoming','xiaohong']]
print(name_list)
```
- extend():列表结尾追加数据,如果数据是一个序列,则这个序列的数据逐一添加到列表,
1. 语法
```python
列表序列.extend(数据)
```
2. 快速体验
2.1单个数据
```python
name_list = ['Tom','Lily','Rose']
name_list.extend('xiaoming')
# 结果:['Tom','Lily','Rose','x','i','a','o','m','i','n','g']
print(name_list)
```
2.2序列数据
```python
name_list = ['Tom','lily','Rose']
name_list.extend(['xiaoming','xiaohong'])
# 结果:['Tom', 'lily', 'Rose', 'xiaoming', 'xiaohong']
print(name_list)
```
- insert():指定位置新增数据
1.语法
```python
列表序列.insert(位置下标,数据)
```
2.快速体验
```python
name_list = ['Tom','Lily','Rose']
name_list.insert(1,'xiaoming')
# 结果:['Tom','xiaoming','Lily','Rose']
print(name_list)
```
### 3.3删除
---
- del
1.语法
```python
del 目标
```
2.快速体验
2.1删除列表
```python
name_list = ['Tom','Lily','Rose']
# 结果:报错提示:name 'name_list' is not defined
del name_list
print(name_list)
```
2.2删除指定数据
```python
name_list = ['Tom','Lily','Rose']
del name_list[0]
# 结果:['Lily','Rose']
print(name_list)
```
- pop():删除指定位置下标的数据(默认为最后一个),并返回该数据
1.语法
```python
列表序列.pop(下标)
```
2.快速体验
```python
name_list = ['Tom','Lily','Rose']
del_name = name_list.pop(1)
# 结果:Lily
print(del_name)
# 结果:['Tom','Rose']
print(name_list)
```
- remove():移除列表中的某个数据的第一个匹配项
1.语法
```python
列表数据.remove(数据)
```
2.快速体验
```python
name_list = ['Tom','Lily','Rose']
name_list.remove('Rose')
# 结果:['Tom','Lily']
print(name_list)
```
- clear():清空列表
```python
name_list = ['Tom','Lily','Rose']
name_list,clear() # -- 清空
print(name_list)
```
### 3.4修改
---
- 修改指定下标的数据
```python
name_list = ['Tom','Lily','Rose']
name_list[0] = 'aaa'
# 结果:['aaa','Lily','Rose']
print(name_list)
```
- 逆置:reverse()
```python
num_list = [1,5,2,3,6,8]
num_list.reverse()
# 结果:[8,6,,3,2,5,1]
print(num_list)
```
- 排序:sort()
1.语法:
```python
列表数据.sort(key=None,reverse=False)
```
- 注意:reverse表示排序规则,reverse=True 降序,reverse = False升序(默认)
2.快速体验
```python
# 3.sort() 排序:升序 和 降序
# list1.sort()
# list1.sort(reverse=True) # 降序排序
# print(list1)
```
### 3.5 复制
---
函数:copy()
```python
name_list = ['Tom','Lily','Rose']
name_li2 = name_list.copy()
# 结果:['Tom','Lily','Rose']
print(name_li2)
```