Suppose I have a list:


Now I want to convert this list into a tuple. I thought coding something like this would do:

for i in a:  

and it gave an error. It’s quite obvious why, I am trying to concatenate an integer with a tuple.

But tuples don’t have the same functions as lists do, like insert or append.
So how can I add elements through looping? Same thing with dictionaries, I feel as if I have a missing link.

Tuples are immutable, you cannot append, delete, or edit them at all. If you want to turn a list into a tuple, you can just use the tuple function:


If, for some reason, you feel the need to append to a tuple (You should never do this), you can always turn it back into a list, append, then turn it back into a tuple:


Keep getting votes for this, which means people keep seeing it, so time to update and remove misinformation.

It’s OK to add elements to a tuple (sort of). That was a silly thing to say. Tuples are still immutable, you can’t edit them, but you can make new ones that look like you appended by putting multiple tuples together. tuple(list(a)+b) is stupid, don’t do that. Just do tuple1 + tuple2, because Python doesn’t suck. For the provided code, you would want:

state = ()  
for i in a:  
    state += (i,)

The Paul’s response to this answer is way more right than this answer ever was.

Now I can stop feeling bad about this.

for i in a:  

The above code will work out to concatenate each time a new tuple (i,) into tuple state.

I am using python 2.7.9.

>>> a = [1,2,3,4,5]
>>> tuple(a)
(1, 2, 3, 4, 5)

just a kind of precis: (2,) is actually same of tuple([2]) so you can write:

>>>(2,) + tuple([2,3])
(2, 2, 3)
>>> tuple([2]) + (2,3)
(2, 2, 3)
>>> tuple([2]) + tuple([2,3])
(2, 2, 3)

python is very supple indeed

tuple is not mutable in python.

so after you initial it with tuple(…), it can’t be modified.

a = [1,2,3,4,5]

If you want a generalized solution, just build up a new list and then convert that to a tuple once finished.

You could incrementally build up to your tuple using the solution provided in Josia’s and byron’s answers, but that will create a new tuple for each iteration, which is very inefficient if the list you want to convert is large, because the interpreter will have to copy all the elements from the old tuple over to the new tuple, in every iteration, which requires O(n^2) operations.

This doesn’t happen with lists, since python will actually allocate more memory than required to just store the elements of the list, just in case you append. Well, at least it is not necessary until that memory runs out and python has to allocate more memory. Then it will allocate a large chunk of memory and move over all elements to the new location, but once again this new piece of memory will be larger than required to just store the elements of the list. Building up a list this way just requires O(n) operations and is therefore to prefer.

After having similar problems building a C/NN with flexible tensor shapes, I think I built the most general solution to this problem:

def cat(*args):
  x = ()
  for a in args:
    if type(a) is tuple:
    elif type(a) in {list,numpy.ndarray}:
  return tuple(x)


In [0]: cat(1,(1,2),(3,4),[1,2],2)
Out[0]: (1, 1, 2, 3, 4, 1, 2)

Not sure if this is bad python, I’m new 🙂