removing an instance of an object in python list

前端 未结 3 741
北恋
北恋 2020-12-11 01:13

I Think this should work but its giving me an error. I have a list that contains objects of class node. I have two different lists

  1. open_list
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 01:58

    In response to your followup, yes in will check for membership in a list, so:

    if removed in node_list: node_list.remove(removed)
    

    will not give you the error. Alternatively, you could trap the error:

    try:
        node_list.remove(removed)
    except ValueError:
        pass
    

提交回复
热议问题