How to create a tuple of an empty tuple in Python?

后端 未结 3 1420
旧巷少年郎
旧巷少年郎 2020-12-15 15:33

How can I create a tuple consisting of just an empty tuple, i.e. (())? I have tried tuple(tuple()), tuple(tuple(tuple())), tuple

相关标签:
3条回答
  • 2020-12-15 15:39

    The empty tuple is () (or the more-verbose and slower tuple()), and a tuple with just one item (such as the integer 1), called a singleton (see here and here) is (1,). Therefore, the tuple containing only the empty tuple is

    ((),)
    

    Here are some results showing that works:

    >>> a=((),)
    >>> type(a)
    <type 'tuple'>
    >>> len(a)
    1
    >>> a[0]
    ()
    >>> type(a[0])
    <type 'tuple'>
    >>> len(a[0])
    0
    
    0 讨论(0)
  • 2020-12-15 15:40

    tuple() is the only genuine empty tuple, but (), and ((),) create a tuple of length 1 that contains a tuple of length 0 - but not a tuple of length zero itself.

    If you want an answer to "how do I create an empty (or zero length) tuple.... I found this post with the search "how to create an empty tuple", then realised this was not the same question, but could be mistaken for that question (as the search does), so I though I would provide the answer to :

    How do you simply create an empty tuple?

    the original question could mislead you, as the original answers are almost good enough as an empty tuple, but do fail one test.

    (), will create an 'empty' tuple as suggested in previous answers with ((),) which will also work, as will ((( ((( (),))) ))) in fact you can use any number of outer brackets you choose, they just work as brackets. However, python, when printing a tuple, does add one set of outer brackets.

    empty brackets is a non-standard representation of 'no value' and adding the trailing comma makes a tuple from 'no value'. But it is a tuple with a 'no value' entry, not an empty tuple.

    Note: This is not a zero length tuple, as the other examples have also shown. The outer tuple is a tuple with one value, just that value has itself, is the empty tuple. So this creates an empty tuple inside another tuple, and the other tuple is not empty. For a true empty tuple by itself, use tuple() although the (), behaves some what similar, it is not quite correct.

    >>>  a = (),
    >>> type(a)
    <class 'tuple'>
    >>> len(a)
    1
    >>> a
    ((),)
    >>> len(a[0])  # the inside tuple is empty, just not the outside one
    0
    

    Similarly, for a tuple of length 1 but with a value (of zero in the case of b, and "" for the example with c)

    >>> b = 0,
    >>> type(b)
    <class 'tuple'>
    >>> len(b)
    1
    >>>b
    (0,)
    # now with an empty string
    >>> c = "",
    >>> type(c)
    <class 'tuple'>
    >>> len(c)
    1
    >>>c
    ('',)
    >>> len (c[0])  # same len of c[0] as with 'empty' tuple
    0
    

    So the outer brackets are included for displaying a tuple, but not actually part of the tuple, nor needed for creating the tuple.

    However all these brackets methods are not a real empty at the outer level, which is something that also has use cases.

    >>> a = ((),)  # extra brackets just to show same as other answers
    >>> len(a)
    1
    >>> if a:
       print("not empty")
    not empty
    >>> e = tuple()
    >>> len(e)
    0
    >>> type(e)
    <class 'tuple'>
    >>> if e:
       print("not empty")
    
    >>> # note...did not print...so e acts as false as an empty tuple should
    

    So if you really need a genuine empty tuple, use tuple(), but if near enough is all you need, you can use (), or ((),)

    0 讨论(0)
  • 2020-12-15 16:00

    I'm not surprised this (()) didn't work, since the outer parentheses get interpreted as that - parentheses. So (()) == (), just like (2) == 2. This should work, however:

    ((),)
    
    0 讨论(0)
提交回复
热议问题