Puzzling difference between lists and tuples

MRAB python at mrabarnett.plus.com
Sun Sep 20 19:34:38 EDT 2020


On 2020-09-20 23:59, Avi Gross via Python-list wrote:
> There is a simple and obvious way to make sure you have a tuple by invoking the keyword/function in making it:
> 
>>>> a=('first')
>>>> type(a)
> <class 'str'>
> 
>>>> a=("first",)
>>>> type(a)
> <class 'tuple'>
> 
>>>> a=tuple("first")
>>>> type(a)
> <class 'tuple'>
> 
> That seems more explicit than adding a trailing comma. It also is a simple way to make an empty tuple but is there any penalty for using the function tuple()?
> 
[snip]
 >>> tuple("first")
('f', 'i', 'r', 's', 't')

Not the same as ("first",).

A simpler way to make an empty tuple is just ().


More information about the Python-list mailing list