Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
deleting from a set
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
did you try set.discard()?
Quote:s.discard(x) - removes x from set s if present
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020