>>> list=['a','b']
>>> tuple=tuple(list)
>>> list.append('a')
>>> print(tuple)
('a', 'b')
>>> another_tuple=tuple(list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable
Why cannot I convert the list 'list' to a tuple?
Do not name variables after classes. In your example, you do this both with list
and tuple
.
You can rewrite as follows:
lst = ['a', 'b']
tup = tuple(lst)
lst.append('a')
another_tuple = tuple(lst)
Explanation by line
The code you posted does not work as you intend because:
another_tuple=tuple(list)
, Python attempts to treat your tuple
created in the second line as a function.tuple
variable is not callable.TypeError: 'tuple' object is not callable
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With