Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: why does a 1-element tuple in python require comma?

  1. #1
    Join Date
    Aug 2012
    Beans
    623

    why does a 1-element tuple in python require comma?

    Hello,
    Why is the comma in tup1 = (1,); required for a single element tuple in python?

    Code:
    #tup1 = (1);
    tup1 = (1,);
    print "tup1[0]: ", tup1[0]
    Thanks.

  2. #2
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: why does a 1-element tuple in python require comma?

    Because otherwise it's ambiguous with the arithmetic expression (1), which is 1 with some extra parentheses.
    LambdaGrok. | #ubuntu-programming on FreeNode

  3. #3
    Join Date
    Aug 2012
    Beans
    623

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by CptPicard View Post
    Because otherwise it's ambiguous with the arithmetic expression (1), which is 1 with some extra parentheses.
    hmm...
    CptPicard, Sorry I'm being a bit slow here. Say in a data structure like array in any programming language, you want to have only one value, you would just assign the value alone right ? Something like
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
     int a[] = {1};
    
    
     printf("%d",a[0]);
    
    
     return 0;
    }
    How is it different in tuples?

  4. #4
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: why does a 1-element tuple in python require comma?

    Go to the Python shell and type (1). What do you get? How could you tell the difference between the expression of a parenthesized 1 and a single-element tuple if the latter also looks like (1)? You couldn't, so you need to define the tuple as something else... for example (1,).
    LambdaGrok. | #ubuntu-programming on FreeNode

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: why does a 1-element tuple in python require comma?

    Corollary: to unpack your "unuple" you need the same kind of dangling comma:
    Code:
    >>> t = (1,)
    >>> e, = t
    >>> e
    1
    Otherwise:
    Code:
    >>> e = t
    >>> e
    (1,)
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  6. #6
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: why does a 1-element tuple in python require comma?

    And an interesting feature of the tuple syntax I had not thought of before: The tuple actually seems to be just the comma-separated list of things, not the parenthesis around it. So the tuple (1,2) can be written as just "1,2".
    LambdaGrok. | #ubuntu-programming on FreeNode

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by CptPicard View Post
    And an interesting feature of the tuple syntax I had not thought of before: The tuple actually seems to be just the comma-separated list of things, not the parenthesis around it. So the tuple (1,2) can be written as just "1,2".
    Never wrote a function that does "return x,y" before?
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  8. #8
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by ofnuts View Post
    Never wrote a function that does "return x,y" before?
    Good point. Then again it's been years since I last touched Python
    LambdaGrok. | #ubuntu-programming on FreeNode

  9. #9
    Join Date
    Aug 2012
    Beans
    623

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by ofnuts View Post
    Corollary: to unpack your "unuple" you need the same kind of dangling comma:
    Quote Originally Posted by CptPicard View Post
    Go to the Python shell and type (1). What do you get?
    I did that, I got
    Code:
    >>> a = (1)
    >>> print a;
    1
    >>> b = (1,)
    >>> print b
    (1,)
    How could you tell the difference between the expression of a parenthesized 1 and a single-element tuple if the latter also looks like (1)? You couldn't, so you need to define the tuple as something else... for example (1,).
    But isn't a "parenthesized 1" same as a single-element tuple. I'm assuming that a tuple is the same as an array in C except that "A tuple is an immutable list. A tuple can not be changed in any way once it is created.(from google)"

    As per my understanding, since I think that a "parenthesized 1" is same as a single-element tuple, I still haven't got it completely.

  10. #10
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by IAMTubby View Post
    But isn't a "parenthesized 1" same as a single-element tuple.
    No.
    Code:
    >>> print 1 == (1)
    True
    >>> print 1 == (1,)
    False
    >>> print (1,) == (1,)
    True
    >>> print (1) == (1,)
    False
    I'm assuming that a tuple is the same as an array in C except that "A tuple is an immutable list.
    Using the C analogy, 1 is an int and an arrary of one int is not an int, it is an array.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •