1

I looked at similar questions but I didn't find this topic anywhere.

I want to know what does the tuple (1,) mean in Python?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
EKons
  • 887
  • 2
  • 20
  • 27

1 Answers1

7

From https://wiki.python.org/moin/TupleSyntax

One Element Tuples

One-element tuples look like:

1,

The essential element here is the trailing comma. As for any expression, parentheses are optional, so you may also write one-element tuples like

(1,)

but it is the comma, not the parentheses, that define the tuple.

Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
  • Well, [Multiple Element Tuples](https://wiki.python.org/moin/TupleSyntax#Multiple_Element_Tuples) says the comma is also needed there, even though `1, 2, 3` successfully coerces to `(1, 2, 3)`. – EKons May 19 '16 at 15:09
  • No the page clearly says that for multiple element tuples the trailing comma is optional. – Xiongbing Jin May 19 '16 at 15:12
  • *but the trailing comma is completely optional.* and *but again, it is the commas, not the parentheses, that define the tuple.* seem contradictory to me, tho. – EKons May 19 '16 at 15:15
  • 1
    @ΈρικΚωνσταντόπουλος The page says that for multiple element tuples `The essential elements are the commas **between** each element of the tuple`, so that should answer your question. The only place where trailing comma is essential is the single element case. – Xiongbing Jin May 19 '16 at 15:17