Sometimes, we want to create a tuple with only one element with Python.
In this article, we’ll look at how to create a tuple with only one element with Python.
How to create a tuple with only one element with Python?
To create a tuple with only one element with Python, we should put a comma after the element.
For instance, we write
a = [('a',), ('b',), ('c', 'd')]
to create list a
that has 3 tuples inside.
We add a comma after 'a'
and 'b'
to make them tuples.
Conclusion
To create a tuple with only one element with Python, we should put a comma after the element.