1. 创建一个空的list
>>>a = []
>>>a = list()
2. L.append(object)
append用于在list末尾追加一个新的对象,改变list本身
>>>a = []
>>>a.append(2)
>>>a
[2]
>>>a.append([3,4])
>>>a
[2,[3,4]]
3. L.extend(iterable)
extend可以在list的末尾追加另一个序列中的多个值
>>>a = [1,2,3]
>>>b = [4,5,6]
>>>a.extend(b)
>>>a
[1,2,3,4,5,6]
>>> member = [1,2,3]
>>> member.extend((4,5,6))
>>> member
[1, 2, 3, 4, 5, 6] #可以将tuple里面的元素追加到list
与连接不同的是:extend方法改变了被扩展的list
>>>a = [1,2,3]
>>>b = [4,5,6]
>>>a + b
[1,2,3,4,5,6]
>>>a
[1,2,3]
4. L.insert(index, object)
insert用于将对象插入index之后,改变list本身
>>>numbers = [1,2,3,4,5]
>>>numbers.insert(0,'start')
>>>numbers
['start',1,2,3,4,5]
>>>len(numbers)
6
#当index>len(list)时,自动插入到最后
>>>a = [1, 2, 3]
>>>a.insert(4,4)
>>>a
[1, 2, 3, 4]
>>>a.insert(-5, -1)
>>>a
[-1, 1, 2, 3, 4]
5. L.pop()
pop用于移除list中的一个元素(默认最后一个),并且返回该元素
可指定位置,不存在的位置会报错
>>>a = [1,2,3]
>>>a.pop()
3
>>>a
[1,2]
>>>a.pop(0)
a
>>>a
[2]
>>>a.pop(100)
IndexError: pop index out of range
6. L.remove()
remove用于移除list中某个值的第一个匹配项
没有返回错误
>>>a = ['we','you','me','to','you']
>>>a.remove('me')
>>>a
['we', 'you', 'to', 'you']
>>>a.remove('you')
>>>a
['we','to','you']
>>>a.remove(3)
ValueError: list.remove(x): x not in list
7. L.count(value)
count用来统计某个元素在list中出现的次数
不存在则返回0
>>>a = [1,2,3,4,5,1]
>>>a.count(1)
2
>>>a.count(2)
1
>>>a.count(0)
0
8. L.index(value, [start, [stop]])
index返回某个值在list中第一次出现的索引
>>>a = [1,2,3,1,2,3]
>>>a.index(2)
1
#当这个值不在list中的时候,会触发一个异常
>>>a.index(0)
ValueError: 0 is not in list
9. L.reverse()
reverse将list中的元素反向存放,改变list本身
>>>a = [1,2,3]
>>>a.reverse()
>>>a
[3,2,1]
10. L.sort()
sort将list进行排序,改变list本身
>>>a = [3,-1,4,-3,7,2,0]
>>>a.sort()
>>>a
[-3, -1, 0, 2, 3, 4, 7]
11.L.clear()
清空
>>>a = [1, 2, 3, 4]
>>>a.clear()
>>>a
[]