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

PythonTuples

Python Problem Overview


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!

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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:

((),)

Solution 3 - Python

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 ((),)

Solution 4 - Python

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 = ((), ())

Solution 5 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAli ShakibaView Question on Stackoverflow
Solution 1 - PythonRory DaultonView Answer on Stackoverflow
Solution 2 - Pythonjuanpa.arrivillagaView Answer on Stackoverflow
Solution 3 - Pythoninnov8View Answer on Stackoverflow
Solution 4 - PythonPikamander2View Answer on Stackoverflow
Solution 5 - PythonRoger DahlView Answer on Stackoverflow