extra empty element when removing an element from a tuple

萝らか妹 提交于 2019-12-01 11:26:31

Python uses a trailing comma in case a tuple has only one element:

In [21]: type((1,))
Out[21]: tuple

from the docs:

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).

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

It indicates a one-element tuple, just to prevent confusion.

(1,) is a tuple, while (1) is just the number 1 with unnecessary parentheses.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!