How to remove an element from a list by index

后端 未结 18 2834
闹比i
闹比i 2020-11-22 03:22

How do I remove an element from a list by index in Python?

I found the list.remove method, but say I want to remove the last element, how do I do this?

18条回答
  •  伪装坚强ぢ
    2020-11-22 03:53

    Use the following code to remove element from the list:

    list = [1, 2, 3, 4]
    list.remove(1)
    print(list)
    
    output = [2, 3, 4]
    

    If you want to remove index element data from the list use:

    list = [1, 2, 3, 4]
    list.remove(list[2])
    print(list)
    output : [1, 2, 4]
    

提交回复
热议问题