在Python 2中,清空列表的方法与Python 3相同,主要有以下几种:
1. 使用 `clear()` 方法:
```python
list1 = ['Google', 'phpcn', 'Taobao', 'Baidu']
list1.clear()
print('列表清空后:', list1)
输出结果:
```
列表清空后: []
2. 使用 `del` 关键字:
```python
list1 = ['Google', 'phpcn', 'Taobao', 'Baidu']
del list1[:]
print('列表清空后:', list1)
输出结果:
```
列表清空后: []
3. 将列表变量重新赋值为空列表:
```python
list1 = ['Google', 'phpcn', 'Taobao', 'Baidu']
list1 = []
print('列表清空后:', list1)
输出结果:
```
列表清空后: []
以上方法都可以在Python 2中清空列表。需要注意的是,`clear()` 方法没有返回值,而使用 `del` 关键字或重新赋值时,列表会被删除,但变量名仍然存在。