Python | 删除列表中第一次出现的给定元素的程序

Python program to remove first occurrence of a given element in the list: Given a list of the elements and we have to remove first occurrence of a given element.

Python程序删除列表中给定元素的第一次出现:给定元素列表,我们必须删除给定元素的第一次出现。

list.remove() (list.remove())

This method removes first occurrence given element from the list.

此方法从列表中删除第一次出现的给定元素。

Syntax:

句法:

 list.remove(element)

Here,

这里,

  • list is the name of the list, from where we have to remove the element.

    list是列表的名称,必须从中删除元素。

  • element an element/object to be removed from the list.

    element从列表中删除的元素/对象。

Example:

例:

    Input :
    list [10, 20, 30, 40, 30]

    function call to remove 30 from the list:
    list.remove(30)

    Output:
    List elements after removing 30
    list [10, 20, 40, 30]

Program:

程序:

# Declaring a list
list = [10, 20, 30, 40, 30]

# print list 
print "List element:"
for l in range(len(list)):
	print (list[l])

# removing 30 from the list 
list.remove(30);

# print list after removing 30
print "List element after removing 30:"
for l in range(len(list)):
	print (list[l])

Output

输出量

    List element:
    10
    20
    30
    40
    30
    List element after removing 30:
    10
    20
    40
    30


翻译自: https://www.includehelp.com/python/remove-first-occurrence-of-a-given-element-in-the-list.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值