s = set('ABC')
s.add('z')
s.update('BCD')
s.remove('DEF') # error here
s -= set('DEFG')
s = set(‘ABC’) s.add(‘z’) s.update(‘BCD’) s.remove(‘DEF’) # error here s -= set(‘DEFG’)
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As others pointed out,
'DEF', the set member you’re trying to remove, is not a member of the set, andremove, per the docs, is specified as “Raises KeyError if elem is not contained in the set.”.If you want “missing element” to mean a silent no=op instead, just use discard instead of
remove: that’s the crucial difference between thediscardandremovemethods of sets, and the very reason they both need to exist!