List
1 Create
2 Retrieve
list = ['a', 'b', 'c', 'd']
for item in list:
print("index: %s value: %s" % (list.index(item), item))
for index in range(len(list)):
print("index: %s value: %s" % (index, list[index]))
for index, val in enumerate(list):
print("index: %s value: %s" % (index, val))
for index, val in enumerate(list, 2):
print("index: %s value: %s" % (index, val))
3 Update
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = list1.copy()
list4 = list1.copy()
list3.append(list2)
list4.extend(list2)
print(list3)
print(list4)
4 Delete
5 Other
5.1 copy()
list1 = [1, 2, 3]
list2 = list1.copy()
list3 = list1
list2.append(4)
print(list1)
list3.append(4)
print(list1)
Reference
- Python 列表(List) 的三种遍历(序号和值)方法