Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does `(5)` not make a one-item tuple in Python?

Quick answer

In Python, parentheses group an expression; they do not by themselves create a tuple. The comma is what makes a tuple, even when there is only one item: (5) is an integer, while (5,) is a one-item tuple. Check the difference with type((5)) is int and type((5,)) is tuple. The empty tuple is written ().[S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does (5) give me an integer instead of a one-item tuple? I thought putting a value in parentheses made a tuple.

Geez

The parentheses group the value 5; they are not what makes a tuple. In Python, the comma marks a tuple, so (5) evaluates to the integer 5, while (5,) evaluates to a tuple containing that integer.[S1]

Here is the minimal comparison:

number = (5)
one_item_tuple = (5,)

print(type(number) is int)              # True
print(type(one_item_tuple) is tuple)    # True

The first check asks whether number is an integer; the second asks whether one_item_tuple is a tuple. Both expressions evaluate to True.[S1]

Follow-up question

Is the comma really enough? Does it still work if I leave out the parentheses?

Geez

Yes. The comma is what makes the tuple, so 5, is also a one-item tuple.[S1] Parentheses can make the intended grouping easier to see; (5,) is an explicit way to write the one-item tuple, while (5) means the grouped integer 5.[S1]

(5)   # grouped expression: the integer 5
(5,)  # tuple display: a tuple with one item

If you need a tuple, include the trailing comma. If you only need the number, (5) is valid grouping, though parentheses are not necessary in a simple assignment.[S1]

Follow-up question

I have a function that accepts a tuple, and I accidentally passed (5). How can I catch that mistake?

Geez

Check the value's type in a small test. This example shows the mistaken form and the corrected one:

mistaken = (5)
correct = (5,)

print(type(mistaken) is int)    # True
print(type(correct) is tuple)   # True
print(len(correct))             # 1

The type checks show the difference, and len(correct) shows that the tuple has one item.[S1] If a function requires a tuple, pass correct (or write (5,) directly) rather than (5). Check that function's own requirements separately; the tuple syntax does not tell you what a particular function accepts.

Follow-up question

What about an empty tuple? Is that another case where the comma is optional?

Geez

The empty tuple is a special case written ().[S1] For one item, use (item,); for no items, use ().[S1]

empty = ()
one_item = (5,)
not_a_tuple = (5)

print(type(empty) is tuple)       # True
print(type(one_item) is tuple)    # True
print(type(not_a_tuple) is int)   # True

The checks distinguish the empty tuple, the one-item tuple, and the grouped integer.[S1] With exactly one item, keep the trailing comma so the expression is a tuple rather than just a grouped value.

A practical checklist

  • If the value should be a one-item tuple, write (value,) with a trailing comma.[S1]

  • Do not rely on parentheses alone to create a tuple; (value) groups the expression.[S1]

  • Use type(value) is tuple in a small check when you need to verify the resulting type.

  • Write the empty tuple as ().[S1]

  • Check the receiving function's own requirements; tuple syntax alone does not show what that function accepts.

A few more useful answers

Is `(5,)` different from `(5)` only because of its type?

Yes: (5) evaluates to the integer 5, while (5,) evaluates to a one-item tuple containing 5.[S1] They are different kinds of values, not two spellings of the same tuple.

Can I write a one-item tuple without parentheses?

Yes. The comma creates the tuple, so 5, is a one-item tuple too.[S1] Parentheses can make the intended grouping clearer; (5,) is an explicit form.

How do I write a tuple with no items?

Use (). The empty tuple is a special case; a one-item tuple needs a trailing comma, as in (5,).[S1]

Sources

References used for this guide.

  1. [S1] Python Language Reference: Parenthesized forms and tuple displays
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work