Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:03:39+00:00 2026-05-28T18:03:39+00:00

According to my interpretation of Python 2.7.2 documentation for Built-In Types 5.7 Set Types

  • 0

According to my interpretation of Python 2.7.2 documentation for Built-In Types 5.7 Set Types, it should be possible to remove the elements of set A from set B by passing A to set.remove(elem) or set.discard(elem)

From the documentation for 2.7.2:

Note, the elem argument to the __contains__(), remove(), and discard()
methods may be a set.

I interpret this to mean that I can pass a set to remove(elem) or discard(elem) and all those elements will be removed from the target set. I would use this to do something weird like remove all vowels from a string or remove all common words from a word-frequency histogram. Here’s the test code:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [M...
Type "help", "copyright", "credits" or "license"
>>> a = set(range(10))
>>> b = set(range(5,10))
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> b
set([8, 9, 5, 6, 7])
>>> a.remove(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: set([8, 9, 5, 6, 7])
>>> a.discard(b)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>>

Which I expect to return:

>>> a
set([0, 1, 2, 3, 4])

I know I can accomplish this with a.difference(b) which returns a new set; or with a set.difference_update(other); or with set operators a -= b, which modify the set in-place.

So is this a bug in the documentation? Can set.remove(elem) actually not take a set as an argument? Or does the documentation refer to sets of sets? Given that difference_update accomplishes my interpretation, I’m guess the case is the latter.

Is that unclear enough?

EDIT
After 3 years of additional (some professional) python work, and being recently drawn back to this question, I realize now what I was actually trying to do could be accomplished with:

>>> c = a.difference(b)
set([0,1,2,3,4])

which is what I was originally trying to get.

EDIT
After 4 more years of python development… I realize this operation can be expressed more cleanly using set literals and the - operator; and that it is more complete to show that set difference is non-commutative.

>>> a={0,1,2,3}
>>> b={2,3,4,5}
>>> a-b
set([0, 1])
>>> b-a
set([4, 5])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:03:40+00:00Added an answer on May 28, 2026 at 6:03 pm

    You already answered the question. It refers to sets of sets (actually sets containing frozensets).

    The paragraph you are referring to begins with:

    Note, the elem argument to the __contains__(), remove(), and discard() methods may be a set.

    which means that b in a.remove(b) can be a set, and then continues with:

    To support searching for an equivalent frozenset, the elem set is temporarily mutated during the search and then restored. During the search, the elem set should not be read or mutated since it does not have a meaningful value.

    which means that if b is a set, a.remove(b) will scan a for a frozenset equivalent to b and remove it (or throw a KeyError if it doesn’t exist).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the Ruby Set class's documentation, == Returns true if two sets are
According documentation: System.Array.Sort<T> - sorts the elements in an entire System.Array using the System.IComparable
According to the CSS 2.1 standard, the vertical margins of floating elements should not
According to developer.apple I should be able to set UISLider's property - thumbTintColor/minimumTrackTintColor/maximumTrackTintColor -
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to
According to select name from system_privilege_map System has been granted: SELECT ANY TABLE ...and
According to the feedparser documentation , I can turn an RSS feed into a
According to Five Simple Database Design Errors You Should Avoid by Anith Sen, using
According to the Doctrine manual I should be able to reference an attribute of
according to the twitter api documentation http://dev.twitter.com/doc/get/statuses/user_timeline accessing the current logged in users timeline

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.