How To Create A Tuple With One Element in Python

Creating a tuple with one element in Python

To create a tuple with one element in Python, you need to either put a comma at the end of the only element in the tuple or pass the list function a list. Read the following article.

Creating a tuple with one element in Python

Tuple in Python is a data type that stores values ​​that do not change later as a constant. Tuples are immutable, so iterating over the tuple’s elements is fast. Therefore, tuple has a slight edge in performance over other data types. Tuples can be used as keys for dictionaries. If data does not change, implementing it as a tuple will ensure it is write-protected. Tuple functions are defined with parentheses. 

Put a comma after the value in the tuple

Example:

  • You want to create a tuple with only one element. It would be best if you put a comma after the variable.
  • Use the type() function to check the variable’s data type.
# creating a tuple with only one element
myTuple = ("learnshareit",)
print('My Tuple:', myTuple)
 
# Check the data type of the variable
print('Datatypes:', type(myTuple))

Output:

My Tuple: ('learnshareit',)
Datatypes: <class 'tuple'>

Note: If you are missing a comma, your variable will be of data type string.

Example:

myTuple = ("learnshareit")
print(myTuple)

# Check the data type of the variable
print('Datatypes:', type(myTuple))

Output:

My Tuple: learnshareit
Datatypes: <class 'str'>

Pass a list to the tuple function

Syntax:

tuple = ( value1, value2, valu3, ….)

Parameters

  • value1, 2, 3: Values ​​belonging to a tuple.

Example:

  • The tuple function takes as an argument a list of only one element. That creates a list consisting of only one element.
  • Use the type function to check the data type of the variable.
# The tuple function parameter is a list of 1 element
myTuple = tuple(['learnshareit']) 
print('My Tuple', myTuple)
 
# Check the data type of the variable
print('Datatypes:', type(myTuple))

Output:

My Tuple ('learnshareit',)
Datatypes: <class 'tuple'>

Suppose you create a multi-element list that is tuples with only one element. In that case, you can split it into 1-element tuples by giving that list as the tuple function parameter and then performing the query. Indexing on tuples will give you tuples with only one element.

Example:

# The tuple function parameter is a list of elements
myTuple = tuple([('learnshareit',), ('website',), (300,)])
 
# Index access on tuple
print('My Tuple:', myTuple[0])
print('My Tuple:', myTuple[1])
print('My Tuple:', myTuple[2])
 
# Check the data type of the variable
print('Datatypes:', type(myTuple))

Output:

My Tuple: ('learnshareit',)
My Tuple: ('website',)
My Tuple: (300,)
Datatypes: <class 'tuple'>

Summary

There are several ways for creating a tuple with one element in Python. You can use the first one: Put a comma after the value in the tuple, because it’s simple. If there are any problems with the article, please leave a comment to let us know. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *