Set literals

Discussion in 'Python' started by George Sakkis, Mar 21, 2005.

  1. How about overloading curly braces for set literals, as in

    >>> aSet = {1,2,3}


    - It is the standard mathematic set notation.
    - There is no ambiguity or backwards compatibility problem.
    - Sets and dicts are in many respects similar data structures, so why not share the same delimiter ?

    *ducks*
    George Sakkis, Mar 21, 2005
    #1
    1. Advertising

  2. > - There is no ambiguity or backwards compatibility problem.

    ....at least if it wasn't for the empty set.. hmm...
    George Sakkis, Mar 21, 2005
    #2
    1. Advertising

  3. George Sakkis

    Guest

    +1 from me.

    The other possible meaning for {1,2,3} would be {1:None,2:None,3:None},
    but that is usually meant to be a set anyway (done with a dict).

    So what is this: {1:2, 3, 4 } (apart from "nearly useless") ?

    hmmm, thinking a bit more about this, it seems
    you can build a set from a dict's keys, but not the other
    way around. Is this odd, or what ?

    >>> a = set({1:0,2:0,3:0})
    >>> a

    set([1, 2, 3])
    >>>
    >>> dict(a)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: cannot convert dictionary update sequence element #0 to a
    sequence

    Simon.
    , Mar 22, 2005
    #3
  4. > wrote:

    > +1 from me.
    >
    > The other possible meaning for {1,2,3} would be {1:None,2:None,3:None},
    > but that is usually meant to be a set anyway (done with a dict).
    >
    > So what is this: {1:2, 3, 4 } (apart from "nearly useless") ?


    Syntax error; you'll have to decide whether you want a set or a dict.

    > hmmm, thinking a bit more about this, it seems
    > you can build a set from a dict's keys, but not the other
    > way around. Is this odd, or what ?
    >
    > >>> a = set({1:0,2:0,3:0})
    > >>> a

    > set([1, 2, 3])
    > >>>
    > >>> dict(a)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: cannot convert dictionary update sequence element #0 to a
    > sequence
    >
    > Simon.


    Nothing odd here. The set constructor takes an iterable, and dict.__iter__ iterates through the
    dict's keys, so the set a in your example doesn't know anything about the dict's values. You can go
    back and forth a set and a dict if you store the dict's items instead:
    >>> a = set({1:0,2:0,3:0}.iteritems())
    >>> a

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

    {1:0, 2:0, 3:0}

    Regards,
    George
    George Sakkis, Mar 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Al Wilkerson

    literals

    Al Wilkerson, Sep 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,593
    Jonathan Allen
    Sep 25, 2004
  2. Replies:
    15
    Views:
    1,294
    Jerry Coffin
    Feb 1, 2005
  3. John Goche
    Replies:
    8
    Views:
    16,430
  4. Delaney, Timothy C (Timothy)

    RE: Set literals

    Delaney, Timothy C (Timothy), Mar 21, 2005, in forum: Python
    Replies:
    1
    Views:
    300
    George Sakkis
    Mar 22, 2005
  5. Steve Howe
    Replies:
    0
    Views:
    204
    Steve Howe
    Oct 24, 2010
Loading...

Share This Page