[Tutor] Sets question

eryk sun eryksun at gmail.com
Wed Apr 26 21:15:23 EDT 2017


On Thu, Apr 27, 2017 at 12:33 AM, Phil <phil_lor at bigpond.com> wrote:
> Another question I'm afraid.
>
> If I want to remove 1 from a set then this is the answer:
>
> set([1,2,3]) - set([1])

You can also use set literals here, with the caveat that {} is
ambiguous, and Python chooses to make it an empty dict instead of a
set.

    >>> {1, 2, 3} - {1}
    {2, 3}

related operations:

    >>> s = {1, 2, 3}
    >>> s -= {1}
    >>> s
    {2, 3}
    >>> s.remove(2)
    >>> s
    {3}

> Ideally, I would like {'1'} to become {1}. Try as I may, I have not
> discovered how to remove the '' marks. How do I achieve that?

The quotation marks are the representation of a string object (i.e.
str). If you want an integer object (i.e. int), you have to convert
the value of the string to an integer.

    >>> x = '1'
    >>> {1, 2, 3} - {int(x)}
    {2, 3}


More information about the Tutor mailing list