Creating a tuple with one element in Python


Borislav Hadzhiev

Last updated: Jun 27, 2022


Check out my new book

Creating a tuple with one element in Python #

Use a trailing comma to create a tuple with one element, e.g. my_tuple = (100,). A tuple with one item is constructed by following a value with a comma. Enclosing the value in parenthesis is not sufficient to create a tuple.
my_tuple_1 = (100,) print(my_tuple_1) # 👉️ (100,) print(type(my_tuple_1)) # 👉️ <class 'tuple'> my_tuple_2 = 100, print(my_tuple_2) # 👉️ (100,) print(type(my_tuple_2)) # 👉️ <class 'tuple'>

We used a trailing comma to create a tuple with 1 item.

As the documentation states construction of tuples with 1 item is done by following a value with a comma.

Note that enclosing the value in parenthesis is not required, but makes your code more readable.

For example, both of these variables store a tuple with 1 item only.
my_tuple_1 = (100,) my_tuple_2 = 100,

It is not sufficient to wrap a single value in parenthesis to create a tuple.

The trailing comma after the value is the difference between creating a tuple (with trailing comma) or creating a value of a different type (without trailing comma).
my_tuple_1 = (100,) print(my_tuple_1) # 👉️ (100,) print(type(my_tuple_1)) # 👉️ <class 'tuple'> my_int = (100) print(my_int) # 👉️ 100 print(type(my_int)) # 👉️ <class 'int'>

The first example uses a trailing comma after the value, so we end up creating a tuple.

The second example simply encloses a value in parenthesis, so we end up creating an integer.

Tuples are constructed in multiple ways:

  • Using a pair of parenthesis () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

The same syntax should be used when creating a list of tuples or using tuples as dictionary keys or values.
my_list = [(100,), (200,), (300,)] print(my_list[0]) # 👉️ (100,) print(type(my_list[0])) # 👉️ <class 'tuple'>

Alternatively, you can use the tuple() class.

Use the tuple() class to create a tuple with one item, e.g. my_tuple = tuple([100]). The tuple() class takes an iterable as an argument and constructs a tuple from the iterable.
my_tuple = tuple([100]) print(my_tuple) # 👉️ (100,) print(type(my_tuple)) # 👉️ <class 'tuple'>

The tuple class takes at most 1 argument - an iterable.

If you aren't sure what type a variable stores, use the built-in type() class.
my_tuple_1 = (100,) print(type(my_tuple_1)) # 👉️ <class 'tuple'> print(isinstance(my_tuple_1, tuple)) # 👉️ True my_int = (100) print(type(my_int)) # 👉️ <class 'int'> print(isinstance(my_int, int)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.