How to remove an element from a list by index

后端 未结 18 3311
闹比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:49

    You could just search for the item you want to delete. It is really simple. Example:

        letters = ["a", "b", "c", "d", "e"]
        letters.remove(letters[1])
        print(*letters) # Used with a * to make it unpack you don't have to (Python 3.x or newer)
    

    Output: a c d e

提交回复
热议问题