67

How can I create a tuple consisting of just an empty tuple, i.e. (())? I have tried tuple(tuple()), tuple(tuple(tuple())), tuple([]) and tuple(tuple([])) which all gave me ().

The reason that I use such a thing is as follows: Assume you have n bags with m items. To represent a list of items in a bag, I use a tuple of length n where each element of that tuple is a representative for a bag. A bag might be empty, which is labeled by (). Now, at some initial point, I have just one bag with empty items!

9
  • 16
    Empty tuple literal is (), not (,)
    – vaultah
    Commented Jun 18, 2016 at 9:46
  • 2
    A tuple with 5 tuples inside it is ((),)*5.
    – Rahul
    Commented Jun 18, 2016 at 10:01
  • 2
    @jeffery_the_wind This does not create the one I want, but thanks for offering lists. It helped me in the following working code tuple([tuple()]). Also, the following answers are just tuple based which much desirable. Commented Jun 18, 2016 at 10:03
  • 3
    @DeepSpace: An empty tuple isn't useless, because unlike None, you can still perform sequence operations like any() and len() on it. The need to test for None can clutter up otherwise elegant iterating loops, and not testing for it can cause unwanted exceptions. Commented May 12, 2017 at 18:54
  • 4
    Aside from the comma confusion, the key insight here is that the tuple() constructor is a converter, not a container. That is, you have to give it some kind of container (technically an iterator). That's why tuple(1) is a TypeError. So while tuple() is indeed an empty tuple, tuple(tuple()) is just that converted to another empty tuple.
    – Bob Stein
    Commented Oct 20, 2017 at 19:43

5 Answers 5

101

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
5
  • This is the correct answer to the actual question, but caution, this does not show how to create a simple empty tuple. If you came looking just an empty tuple, not 'a tuple that contains an empty tuple' then use caution!
    – innov8
    Commented Oct 31, 2018 at 23:11
  • 9
    @innov8: My answer opens with "The empty tuple is ()". It does indeed "show how to create a simple empty tuple." It then goes on to also show how to "create a tuple consisting of just an empty tuple." Commented Oct 31, 2018 at 23:12
  • @rory_daulton your answer is a perfect answer to the question. My point is that you can land on this page for another question 'how to create an empty tuple' - which is different question. Of course you did not focus on a question which was not asked!
    – innov8
    Commented Nov 1, 2018 at 0:18
  • 1
    @innov8, but he did answer 'how to create an empty tuple'. You use () - which denotes the empty tuple: docs.python.org/3.3/library/stdtypes.html?highlight=tuple
    – Joe
    Commented Sep 29, 2020 at 22:56
  • 2
    The outer parenthesis are not required in a=((),). a=(), is enough, as it's the commas, not parenthesis, that makes non-empty tuples.
    – Roger Dahl
    Commented Jan 22, 2022 at 21:10
10

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
5

in Python 2, 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 realized 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 ((),)

4
  • 1
    part of this seems to be valid only for python 2, in python 3 a = (); len(a) returns 0
    – Jeffrey04
    Commented Mar 22, 2019 at 10:41
  • 1
    yes, you must use a=(),;len(a) as without the trailing comma, you will not get the desired result. You get a tuple, not a tuple within a tuple
    – innov8
    Commented Mar 27, 2019 at 5:17
  • Why do you say that "tuple() is the only genuine empty tuple"? What about just using ()?
    – Joe
    Commented Sep 29, 2020 at 22:52
  • @Joe, () certainly works in current versions, although i do feel it is not as clear about what it is doing, not sure why i did not include it back in Oct 2018
    – innov8
    Commented Oct 19, 2020 at 3:09
4

An empty tuple:

my_tuple = ()

A tuple with 1 string:

my_tuple = ('foo',)

A tuple with 2 strings:

my_tuple = ('foo', 'bar')

A tuple with 1 empty tuple:

my_tuple = ((),)

A tuple with 2 empty tuples:

my_tuple = ((), ())
2
  • 1
    Note that the outer parenthesis are not needed in the examples that have commas in them.
    – Roger Dahl
    Commented Jan 22, 2022 at 21:05
  • 2
    @RogerDahl - That's true, although personally I think that including the outer parentheses makes it easier to understand at a glance. Commented Jan 23, 2022 at 17:50
2

In the general case, it's the commas that make tuples, not the parentheses. Things become confusing in the case of empty tuples because a standalone comma is syntactically incorrect. So for the special case of an empty tuple, the "it is commas that make tuples" rule does not apply, and the special case () syntax is used instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.