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!
You can't add a tuple and number. Your line should probably be newtup += (aTup[i],) . Haven't checked it though. Adding the parentheses and comma turns the number into a tuple with a single member.
You can create empty tuple object by giving no elements in parentheses in assignment statement. Empty tuple object is also created by tuple() built-in function without any arguments >>> T1 = () >>> T1 () >>> T1 = tuple() >>> T1 () Pythonic. © Copyright 2022. All Rights Reserved.
To create an empty tuple, use empty parenthesis or the tuple() function. Using empty parenthesis is faster and usually more efficient compared to using the tuple() function. It is also considered more pythonic and the recommended way of creating empty tuples.
Explanation: An Empty Tuple has 48 Bytes as Overhead size and each additional element requires 8 Bytes.
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
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:
((),)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With