要保留第二个列表的名称并取消列表的列表,可以使用以下代码示例:
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
# 将list2的元素添加到list1中
list1.extend(list2)
# 清空list2
list2.clear()
# 输出结果
print("List 1:", list1)
print("List 2:", list2)
输出结果如下:
List 1: [1, 2, 3, 4, 5, 6, 7, 8]
List 2: []
在这个示例中,我们使用extend()
方法将list2的元素添加到list1中,这样可以保留list1的名称。然后,使用clear()
方法清空list2,使其成为空列表。最后,我们输出两个列表的结果,可以看到list1包含了list2的元素,而list2变为空列表。