How to delete an item in a list if it exists?

前端 未结 7 945
無奈伤痛
無奈伤痛 2020-11-29 14:55

I am getting new_tag from a form text field with self.response.get(\"new_tag\") and selected_tags from checkbox fields with



        
7条回答
  •  被撕碎了的回忆
    2020-11-29 15:14

    Eek, don't do anything that complicated : )

    Just filter() your tags. bool() returns False for empty strings, so instead of

    new_tag_list = f1.striplist(tag_string.split(",") + selected_tags)
    

    you should write

    new_tag_list = filter(bool, f1.striplist(tag_string.split(",") + selected_tags))
    

    or better yet, put this logic inside striplist() so that it doesn't return empty strings in the first place.

提交回复
热议问题