python convert list to tuple

I’m trying to convert a list to a tuple. When i google it I find a lot of a answers like:

l = [4,5,6]

But if I do that I get an error message:

TypeError: ‘tuple’ object is not callable

I’m using python 2.7.3

How can I convert Rpy2 data frame to python list or tuple?

I have a Rpy2 data frame as <class ‘rpy2.robjects.vectors.DataFrame’>. How can I convert it to a Python list or tuple with every row as an element? Thanks!

Python tuple instead of list

I read some tricks about Python and met the following code. I confused that why the code create tuple with two elements in the list insted of list. Why python returns tuple in list instead of list in

Python convert tuple to array [duplicate]

This question already has an answer here: Making a flat list out of list of lists in Python [duplicate] 8 answers How can I convert at 3-Dimensinal tuple into an array a = [] a.append((1,2,4))

Convert a string list to a tuple in Python

I am trying to convert a string such as ‘SUP E P I C’ to a tuple containing all the non-spaced strings. For example, if the input is ‘SUP E P I C’, then the program should return (‘S’, ‘U’, ‘P’, ‘E’,

Fast way to convert array to tuple/list in python?

It seems like the time it takes to convert an array to a tuple scales linearly with the the length of the array. Is there a way to do this more efficient? I need to insert arrays with 5e+6 elements in

Python convert Tuple to Integer

Is there any function that can convert a tuple into an integer? Example: input = (1, 3, 7) output = 137

Convert List of Lists into Tuple

How can I convert a list of lists [[a]] into a tuple ([a], [a])? Example: input: [[1], [2,3,4]] output: ([1], [2,3,4])

Python tuple list

Can You help me to convert Python list: [(1, ‘a’), (2, ‘b’), (2, ‘c’), (3, ‘d’), (3, ‘e’)] so that: (1, ‘a’) is index 0 (2, ‘b’), (2, ‘c’) are both index 1 (3, ‘d’), (3, ‘e’) are both index 2 Simply,

Python convert dictionary into tuple

How do I convert a dictionary into a tuple? Below is my dynamic dictionary. genreOptions = GenreGuideServiceProxy.get_all_genres(); genreDictionary = {}; for genre in genreOptions: genreDictionary[gen

Python convert tuple to string

I have a tuple of characters like such: (‘a’, ‘b’, ‘c’, ‘d’, ‘g’, ‘x’, ‘r’, ‘e’) How do I convert it to a string so that it is like: ‘abcdgxre’


It should work fine, don’t use tuple, list or other special names as a variable name. It’s probably whats causing your problem.

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

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'>

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>
TypeError: 'tuple' object is not callable
>>> del tuple  # you can delete 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..

l = [4,5,6]

to convert list to tuple,

l = tuple(l)

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","pytho"]
>>> l
[1, 2, 'stackoverflow', 'pytho']
>>> tup = tuple(l)
>>> type(tup)
>>> tup = tuple(l)
>>> type(tup)
<type 'tuple'>
>>> type(tup)
<type 'tuple'>
>>> tup
(1, 2, 'stackoverflow', 'pytho')

smart way

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

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,will also make your program efficient.