Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I'm trying to convert a list to a tuple.

When I google it, I find a lot of answers similar to:

l = [4,5,6]

tuple(l)

But if I do that I get this error message:

TypeError: 'tuple' object is not callable

How can I fix this problem?

1 Answer

0 votes
by (106k points)

The following mentioned code will work fine for converting list to a tuple in Python. You should keep one thing in mind that at the time of writing the code do not use tuple, list or other special names as a variable name. 

l = [4,5,6]

tuple(l)

image

Now discussing why you are getting that error the reason is, you've redefined tuple to be a tuple rather than the type tuple like as follows:

tuple = tuple(l)

tuple 

Now you get a TypeError since the tuple itself is not callable:

tuple(l)

TypeError: 'tuple' object is not callable

To overcome this error, You will have to recover the original definition for tuple by quitting and restarting your interpreter:-

del tuple

tuple 

Output:- <type 'tuple'>

Related questions

0 votes
2 answers
asked Sep 17, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...