要删除列表的第一个元素,可以使用'del'关键字或者'pop()'方法。

使用'del'关键字:

my_list = [1, 2, 3, 4, 5]
del my_list[0]
print(my_list)  # 输出 [2, 3, 4, 5]

使用'pop()'方法:

my_list = [1, 2, 3, 4, 5]
my_list.pop(0)
print(my_list)  # 输出 [2, 3, 4, 5]

这两种方法都会删除列表中的第一个元素,并返回删除的元素。

Python 列表删除第一个元素:del 和 pop 方法

原文地址: https://www.cveoy.top/t/topic/dUlH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录