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 ((),)
()
, not(,)
– Thresathresha = [[]]
– Euxenite((),)*5
. – Chemoprophylaxislists
. It helped me in the following working codetuple([tuple()])
. Also, the following answers are just tuple based which much desirable. – SkimmiaNone
, you can still perform sequence operations likeany()
andlen()
on it. The need to test forNone
can clutter up otherwise elegant iterating loops, and not testing for it can cause unwanted exceptions. – Tetertuple()
constructor is a converter, not a container. That is, you have to give it some kind of container (technically an iterator). That's whytuple(1)
is a TypeError. So whiletuple()
is indeed an empty tuple,tuple(tuple())
is just that converted to another empty tuple. – Hippel