Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there more ways to define a tuple with only one item?

I know this is one way, by placing a comma:

>>> empty = ()
>>> singleton = 'hello',    # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)

Source: http://docs.python.org/tutorial/datastructures.html

Are there more ways to define a tuple with only 1 item?

like image 855
Alphonse Avatar asked Feb 17 '10 14:02

Alphonse


People also ask

How many ways are there to make a tuple?

Initialize a Tuple There are two ways to initialize an empty tuple. You can initialize an empty tuple by having () with no values in them. You can also initialize an empty tuple by using the tuple function. A tuple with values can be initialized by making a sequence of values separated by commas.

How do you define a tuple?

Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.


1 Answers

>>> tuple(['hello'])
('hello',)

But the built-in syntax is there for a reason.

like image 154
Josh Lee Avatar answered Oct 19 '22 23:10

Josh Lee