Categories
Python Answers

How to create a tuple with only one element with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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