How to create a tuple of an empty tuple in Python?
Asked Answered
S

5

66

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!

Skimmia answered 18/6, 2016 at 9:45 Comment(9)
Empty tuple literal is (), not (,)Thresathresh
@Thresathresh corrected. :)Skimmia
"Now, at some initial point, I have just one bag with empty items!" Note that tuple is immutable, so an empty tuple is basically useless as you can't add items to it later on. If you want to, you should consider using a list instead.Dampier
@Dampier That's right. This tuple is then used to create a new tuple and I store all the tuples somewhere for logging.Skimmia
IMO it seems you should be using lists instead of tuples. It is then trivial to create a list of lists, add and remove items from the outer list or inner lists. a = [[]]Euxenite
A tuple with 5 tuples inside it is ((),)*5.Chemoprophylaxis
@Euxenite 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.Skimmia
@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.Teter
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.Hippel
S
99

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
Shatterproof answered 18/6, 2016 at 9:58 Comment(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!Allisan
@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."Shatterproof
@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!Allisan
@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=tupleBradway
The outer parenthesis are not required in a=((),). a=(), is enough, as it's the commas, not parenthesis, that makes non-empty tuples.Surrebutter
A
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:

((),)
Alternation answered 18/6, 2016 at 9:58 Comment(0)
A
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 ((),)

Allisan answered 31/10, 2018 at 22:5 Comment(4)
part of this seems to be valid only for python 2, in python 3 a = (); len(a) returns 0Gonick
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 tupleAllisan
Why do you say that "tuple() is the only genuine empty tuple"? What about just using ()?Bradway
@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 2018Allisan
S
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 = ((), ())
Selfconceit answered 12/1, 2021 at 5:56 Comment(2)
Note that the outer parenthesis are not needed in the examples that have commas in them.Surrebutter
@RogerDahl - That's true, although personally I think that including the outer parentheses makes it easier to understand at a glance.Selfconceit
S
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.

Surrebutter answered 3/6, 2021 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.