Python Forum

Full Version: deleting from a set
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a set which contains many words/strings. i want to delete one. but setname.remove(theword) will raise KeyError if it is not already in there. in my case, it may not. so i can do if word in setname: setname.remove(theword). does that slow it down much? this is going to be in a heavy execution part of my code? any fast way to do this? the number of different words to be deleted is just 4. would it be better to keep the 4 words as 4 sets (each containing 1 word) and doing setname -= thewordset.
did you try set.discard()?
Quote:s.discard(x) - removes x from set s if present
(May-28-2018, 07:31 AM)buran Wrote: [ -> ]did you try set.discard()?
Quote:s.discard(x) - removes x from set s if present
i missed that in the documentation. i wish things were indexed with every method in a table with exactly one line each so things are easier to find.