note.nkmk.me

One-element tuples require a comma in Python

Posted: 2019-09-18 / Tags: Python

Tuples are immutable sequence objects in Python.

Be careful when creating one-element tuples or empty tuples.

Here, the following contents will be described.

  • One-element tuple
  • Tuple parentheses are basically optional
  • Empty tuple
  • Tuples in function arguments
Sponsored Link

One-element tuple

When generating a one-element tuple, if you write only one object in parentheses (), the parentheses () are ignored and not considered a tuple.

single_tuple_error = (0)

print(single_tuple_error)
print(type(single_tuple_error))
# 0
# <class 'int'>

To generate a one-element tuple, a comma , is required at the end.

single_tuple = (0, )

print(single_tuple)
print(type(single_tuple))
# (0,)
# <class 'tuple'>

For example, when connecting multiple tuples with + operator, note that if you try to add one element and forget a comma ,, an error will occur.

# print((0, 1, 2) + (3))
# TypeError: can only concatenate tuple (not "int") to tuple

print((0, 1, 2) + (3, ))
# (0, 1, 2, 3)

Tuple parentheses are basically optional

The reason why you need a comma , for a single tuple is that "tuple is an object delimited by comma ,", not "an object enclosed in parentheses ()".

Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity.
Built-in Types - Tuples — Python 3.7.4 documentation

It is a tuple even if the parentheses () are omitted.

t = 0, 1, 2

print(t)
print(type(t))
# (0, 1, 2)
# <class 'tuple'>

Note that if there is an unnecessary comma , after the object, it will be considered a tuple.

t_ = 0,

print(t_)
print(type(t_))
# (0,)
# <class 'tuple'>
Sponsored Link

Empty tuple

As mentioned above, parentheses () can be omitted to represent a tuple, but are required when creating an empty tuple.

SyntaxError will occur if there is only a space or a comma,.

empty_tuple = ()

print(empty_tuple)
print(type(empty_tuple))
# ()
# <class 'tuple'>

# empty_tuple_error = 
# SyntaxError: invalid syntax

# empty_tuple_error = ,
# SyntaxError: invalid syntax

# empty_tuple_error = (,)
# SyntaxError: invalid syntax

An empty tuple can also be created with tuple() without an argument.

empty_tuple = tuple()

print(empty_tuple)
print(type(empty_tuple))
# ()
# <class 'tuple'>

Tuples in function arguments

If there is a syntactic ambiguity, tuples require parentheses ().

Multiple arguments are specified by separating them with a comma ,. If you want to specify a tuple as one argument, parentheses () are required.

Without parentheses (), each value is passed to each argument, and with parentheses (), a tuple is passed to one argument.

def example(a, b):
    print(a, type(a))
    print(b, type(b))

example(0, 1)
# 0 <class 'int'>
# 1 <class 'int'>

# example((0, 1))
# TypeError: example() missing 1 required positional argument: 'b'

example((0, 1), 2)
# (0, 1) <class 'tuple'>
# 2 <class 'int'>

If you add * to the tuple, you can expand the tuple and pass each element to each argument.

example(*(0, 1))
# 0 <class 'int'>
# 1 <class 'int'>

See the following post for details.

Sponsored Link
Share

Related Categories

Related Posts