Length 1 tuples#

Remember Tuples. For example, here are a couple of length two tuples:

first_tuple = (1, 2)
first_tuple
(1, 2)
second_tuple = (3, 4)

As for lists, you can add tuples, to concatenate the contents:

tuples_together = first_tuple + second_tuple
tuples_together
(1, 2, 3, 4)

Length 1 tuples#

Let us say you want to write code for a tuple with only one element.

You might think this would work:

# Python interprets this parentheses in arithmetic.
just_a_number = (1)
just_a_number
1

Just a single number or string, with parentheses around it, does not make a tuple, because Python interprets this as your usual brackets for arithmetic. That means that:

(1)
1

is exactly the same as:

1
1

Why? Because, Python has to decide what expressions like this mean:

# Wait - what do the parentheses mean?
(1 + 2) + (3 + 4)

Is this adding two one-element tuples, to give a two-element tuple (3, 7)? Or is it our usual mathematical expression giving 3 + 7 = 10. The designer of the Python language decided it should be an arithmetical expression.

# They mean parentheses as in arithmetic.
(1 + 2) + (3 + 4)
10

To form a length-1 tuple, you need to disambiguate what you mean by the parentheses, with a trailing comma:

short_tuple = (1,)  # Notice the trailing comma
short_tuple
(1,)

So, for example, to add two length one tuples together, as above:

# Notice the extra comma to say - we mean these to be length-1 tuples.
(1 + 2,) + (3 + 4,)
(3, 7)