678

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

Most solutions on Google offer the following code:

l = [4,5,6]
tuple(l)

However, the code results in an error message when I run it:

TypeError: 'tuple' object is not callable

How can I fix this problem?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
LynnH
  • 7,216
  • 3
  • 15
  • 13
  • 87
    Did you assign the variable name `tuple` elsewhere before? – eumiro Oct 11 '12 at 09:13
  • 23
    Not to be overly picky, but you probably also shouldn't use lower case "el" as a variable name due to its resemblance to 1. Same goes for capital "oh" due to its resemblance to zero. Even "li" is much more readable in comparison. – Albert Rothman Sep 23 '16 at 18:04
  • @Tomerikoo unless there is a better canonical, perhaps it would be better to edit the question to be a pure how-to rather than dupe-hammering it to something that will be useless for most people who get here from a search engine. Although that would break the top answers somewhat... hmm. – Karl Knechtel Jun 03 '23 at 07:33
  • @KarlKnechtel The accepted answer (and highly voted) is answering the dupe (i.e. the overriding mix-up rather than actually converting a list to a tuple). By the amount of votes, I assume that most people coming here will be just fine with the current dupe. As to the others coming here for the title - the answer is basically in the question... I'm not sure this problem justifies a SO question but that's a different philoSOphical discussion – Tomerikoo Jun 04 '23 at 08:24

8 Answers8

988

It should work fine. Don't use tuple, list or other special names as a variable name. It's probably what's causing your problem.

>>> l = [4,5,6]
>>> tuple(l)
(4, 5, 6)

>>> tuple = 'whoops'   # Don't do this
>>> tuple(l)
TypeError: 'tuple' object is not callable
Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
root
  • 76,608
  • 25
  • 108
  • 120
  • This is retriving an extra value when there is only one element in the list >>> taskid = ["10030"] >>> t = tuple(l) >>> t ('10030',) – Rafa Jul 07 '21 at 02:33
  • @Rafa, the comma is part of the tuple notation when there is only one element, if that's what you mean. – PatrickT Nov 07 '21 at 11:05
122

Expanding on eumiro's comment, normally tuple(l) will convert a list l into a tuple:

In [1]: l = [4,5,6]

In [2]: tuple
Out[2]: <type 'tuple'>

In [3]: tuple(l)
Out[3]: (4, 5, 6)

However, if you've redefined tuple to be a tuple rather than the type tuple:

In [4]: tuple = tuple(l)

In [5]: tuple
Out[5]: (4, 5, 6)

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

In [6]: tuple(l)
TypeError: 'tuple' object is not callable

You can recover the original definition for tuple by quitting and restarting your interpreter, or (thanks to @glglgl):

In [6]: del tuple

In [7]: tuple
Out[7]: <type 'tuple'>
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 2
    Convoluted trivia: even if `tuple` is shadowed, the actual type is still available as `().__class__` as in: `().__class__(range(10))` :) – tzot Nov 25 '20 at 15:47
36

To add another alternative to tuple(l), as of Python >= 3.5 you can do:

t = *l,  # or t = (*l,) 

short, a bit faster but probably suffers from readability.

This essentially unpacks the list l inside a tuple literal which is created due to the presence of the single comma ,.


P.s: The error you are receiving is due to masking of the name tuple i.e you assigned to the name tuple somewhere e.g tuple = (1, 2, 3).

Using del tuple you should be good to go.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
30

You might have done something like this:

>>> tuple = 45, 34  # You used `tuple` as a variable here
>>> tuple
(45, 34)
>>> l = [4, 5, 6]
>>> tuple(l)   # Will try to invoke the variable `tuple` rather than tuple type.

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    tuple(l)
TypeError: 'tuple' object is not callable
>>>
>>> del tuple  # You can delete the object tuple created earlier to make it work
>>> tuple(l)
(4, 5, 6)

Here's the problem... Since you have used a tuple variable to hold a tuple (45, 34) earlier... So, now tuple is an object of type tuple now...

It is no more a type and hence, it is no more Callable.

Never use any built-in types as your variable name... You do have any other name to use. Use any arbitrary name for your variable instead...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
5

I find many answers up to date and properly answered but will add something new to stack of answers.

In python there are infinite ways to do this, here are some instances
Normal way

>>> l= [1,2,"stackoverflow","python"]
>>> l
[1, 2, 'stackoverflow', 'python']
>>> tup = tuple(l)
>>> type(tup)
<type 'tuple'>
>>> tup
(1, 2, 'stackoverflow', 'python')

smart way

>>>tuple(item for item in l)
(1, 2, 'stackoverflow', 'python')

Remember tuple is immutable ,used for storing something valuable. For example password,key or hashes are stored in tuples or dictionaries. If knife is needed why to use sword to cut apples. Use it wisely, it will also make your program efficient.

Granny Aching
  • 1,295
  • 12
  • 37
TheExorcist
  • 1,966
  • 1
  • 19
  • 25
-1

This should work - tuple([x for x in words])

  • This is just the same as `tuple(words)`... Note that this question was asked more than 10 years ago, already heavily answered and essentially caused by a typo. I really don't see the point in answering it – Tomerikoo Feb 16 '23 at 12:52
-2
l1 = []   #Empty list is given

l1 = tuple(l1)   #Through the type casting method we can convert list into tuple

print(type(l1))   #Now this show class of tuple
Mahan Vyakti
  • 129
  • 3
  • 10
-2
  1. Have you assigned the name 'tuple' as a variable name? it should work fine.

L is a list and we want to convert it to a tuple.

L = [1, 2, 3]

tuple(L)

By invoking tuple, you convert the list (L) into a tuple. As done above.

>> (1, 2, 3)

you can go ahead and access any item in the tuple using the square brackets. L[0]

1