89DEVs

Python: How to create an empty tuple?

How to create an empty tuple?

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.

create an empty tuple using parenthesis

To create an empty tuple use parenthesis as shown below. # create an empty tuple using parenthesis myTuple = () print(myTuple) The empty tuple is created and printed. () To check if the new object is a tuple, use the type() function. # check the type of the created object myTuple = () print(type(myTuple)) After checking the type, it is confirmed that a new empty tuple was created.

create an empty tuple using tuple() function

To create an empty tuple in Python, use the tuple() function as shown below. If the tuple() function is called without any argument, an empty tuple is created. If values are passed to the tuple() function, they are used to fill the tuple. Please note, that values have to be within parenthesis to fill the tuple. # create tuples using tuple() function myEmptyTuple = tuple() myNonEmptyTuple = tuple((1, 2, 3, 4)) print(myEmptyTuple) print(myNonEmptyTuple) In this example two tuples are created. The first tuple is an empty tuple, created by calling the tuple() function without passing any values as arguments. The second tuple is a non-empty tuple, created by passing values as arguments to the tuple() function. Finally both tuples are printed. () (1, 2, 3, 4)

add elements to an tuple

To add new elements to an tuple, add the new elements as tuple and combine both tuples as shown below. # add elements to an tuple myTuple = tuple((1, 2, 3)) myTuple = myTuple + ((4,)) print(myTuple) The element is added to the existing tuple and the result is printed. (1, 2, 3, 4) Another way to add elements to a tuple, is converting the tuple to a list, adding the elements to the list and finally converting it back to a tuple. # add elements to an tuple by converting it to a list myTuple = tuple((1, 2, 3)) myList = list(myTuple) myList = myList + [4] myTuple = tuple(myList) print(myTuple) In this code example the first line creates the tuple. The second line converts the tuple into a list. The third line adds the new element to the list. The fourth line converts the expanded list back to a tuple. The fifth line finally prints the expanded tuple. (1, 2, 3, 4)

check if tuple is empty

To check if the created tuple is empty, use the not in operator. # check if tuple is empty myTuple = () if not myTuple: True Here, the tuple myTuple is empty and Python considers it False. By using the if not operator True is returned if the tuple is empty. True Some other way of checking if the tuple is empty is using the len() function to determine how many elements the tuple has. One more valid way is to compare the tuple to be checked to an empty tuple. If this returns True, the tuple is empty. If it returns False, it is not empty. However, the recommended way of checking if a tuple is empty, is using the if not operator as shown above.

                
        

Summary


Click to jump to section