扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python如何删除指定列表元素

扬州沐宇科技
2024-05-27 18:46:07
python

要删除列表中的指定元素,可以使用remove()方法或者pop()方法。

使用remove()方法:

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

使用pop()方法:

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

在remove()方法中,你需要指定要删除的元素的值,而在pop()方法中,你需要指定要删除的元素的索引位置。

扫码添加客服微信