How to create a tuple with one element in Python?

In this tutorial, we are going to learn about how can we create a tuple with only one element. Basically, the tuple is one of the built-in datatypes, used to store the collection of data. We can create a tuple, using the tuple constructor, or using the parentheses as well.

How to create a tuple with one element in Python?

How to create a tuple?

Let’s first have a look at how can we create a tuple –

Way 1 – Creating a tuple using the tuple constructor :

mytuple = tuple()
print(type(mytuple))

As you can see, we are creating a tuple using the tuple constructor. Then, we are checking the type of object that we have created. If you have a look at the output, you can find that we have a tuple.

Let’s now have a look at another way to create a tuple, using the parentheses.

Way 2 – Creating tuple using parentheses:

mytuple = ()
print(type(mytuple))

As you can see in the above program, we are creating a tuple, using the parentheses. After that, here as well, we are checking the type of object that we have created. If you again try to have a look at the output, you can find that we have created a tuple.

The thing is that to create the tuple, making use of parentheses is optional. So, have a look at the below program, where you can find how we have created a tuple.

mytuple = 1, 2, 3, 4, 5
print(type(mytuple))

As you can see, we have created a tuple, and from the above program, we can understand that the parentheses are optional.

Creating a tuple with only one element

Let’s say that we are creating a tuple using parentheses, and we need to have only one element in the tuple. So, in such a situation, someone would try to do something like this –

mytuple = (“GyaniPandit”)
print(type(mytuple))

In the above program, you can simply see that we have made use of the parentheses, and we have one string there. After that, we are trying to check the type of the object. If you try to have a look at the output of the above program, you would find that we do not have a tuple, but instead, we have a string(depending on what is the object that we have in the parentheses. If we had an int, we would get the object as of int type). So, when we are creating a tuple using the parentheses, and we are having only one element in the tuple,

In order to recognize the object as a tuple, we just need to add a comma after the element. Have a look at the below program, which tries to demonstrate the same thing.

mytuple = (“GyaniPandit”, )
print(type(mytuple))

As you can see in the above program, we have just put a comma after the element, and now we have the object as a tuple.

So, if you are creating a tuple with one element, and you are using parentheses, then to recognize the object as a tuple, you just need to add a comma after the element.

Conclusion

In this article, we have seen how can we create a tuple with only one element. In order to create a tuple with only one element, we just need to add a comma after the element. By doing this simple thing, we can create a tuple with one element, when we are using parentheses.

FAQ about How to create a tuple with one element in Python

Q: How to create a tuple with one element?

Ans: If you are having only one element in the tuple, and you are using parentheses, you need to add a comma after the element, in order to recognize the object as a tuple.

Q: What are the different ways to create a tuple?

Ans: There are two ways to create a tuple. We can make use of a tuple constructor, or we can also have elements separated by a comma, within parentheses(The parentheses are optional though)