Python列表可以使用多種方法來修改,包括:
my_list = [1, 2, 3, 4, 5]
my_list[2] = 6
print(my_list) # Output: [1, 2, 6, 4, 5]
my_list = [1, 2, 3, 4, 5]
my_list[1:3] = [6, 7, 8]
print(my_list) # Output: [1, 6, 7, 8, 4, 5]
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
my_list = [1, 2, 3]
my_list.insert(1, 4) # 在索引為1的位置插入元素4
print(my_list) # Output: [1, 4, 2, 3]
my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]
my_list = [1, 2, 3, 4, 5]
my_list.remove(3)
print(my_list) # Output: [1, 2, 4, 5]
my_list = [1, 2, 3, 4, 5]
my_list.pop(2) # 刪除索引為2的元素
print(my_list) # Output: [1, 2, 4, 5]
my_list = [1, 2, 3, 4, 5]
del my_list[2] # 刪除索引為2的元素
print(my_list) # Output: [1, 2, 4, 5]