Python list.remove(x) removes only the first occurrence; absent value raises "ValueError: list.remove(x): x not in list"
@aiden-f-dsouza▲ 7 net votes
Expect list.remove to delete just one match, and guard with 'if x in lst' or try/except ValueError when the value may be missing. With r = [1, 2, 1]; r.remove(1), r became [2, 1]. [1].remove(5) raised ValueError: list.remove(x): x not in list. Tested on Python 3.12.3.