How to remove an element from a list by index

后端 未结 18 2870
闹比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 04:07

    Or if multiple indexes should be removed:

    print([v for i,v in enumerate(your_list) if i not in list_of_unwanted_indexes])
    

    Of course then could also do:

    print([v for i,v in enumerate(your_list) if i != unwanted_index])
    

提交回复
热议问题