42

Suppose I have a list:

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

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

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

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.

2
  • nothings bad .. i wanted to have more control over the list so that i can dynamically add data when ever i want...using tuple(a) would give (1,2,3,4,5) what if i want to add 6,7,8,9,0 in the same list... i wanted a generalised solution
    – user784530
    May 5, 2012 at 6:12
  • 1
    If you want to edit it, a tuple is not your answer, just use a list. The exact point of a tuple is to be an immutable list.
    – Josiah
    May 5, 2012 at 6:12

6 Answers 6

86

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:

tuple(a)

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:

tuple(list(a)+b)

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.

4
  • 33
    "You should never do this" may not be helpful advice, since you can't append to a tuple :), but it is frequently useful to generate a new tuple that is the same as the old one with more elements added. Also, it's not necessary to turn a into a list in your second example; it should probably be just a + tuple(b) or a + (b,) if b is an element to add instead of an iterable.
    – the paul
    May 5, 2012 at 22:35
  • @thepaul Thanks for that syntax. I thought b, + a would work, but it does not -- that's why I wound up on this page. The brackets are needed in this scenario, as you have it: (b,) + a. However attempting to do tuple(b) when b is an integer yields the error 'int' object is not iterable. I'm using Python 2.7.8. Jul 28, 2015 at 15:08
  • 2
    Didn't answer my question, but +1 for coming back and fixing it!
    – Raydot
    Dec 27, 2019 at 20:11
  • 1
    I'm so glad python doesn't suck. Dec 22, 2020 at 17:18
16
state=()  
for i in a:  
    state=state+(i,)

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

I am using python 2.7.9.

6
>>> a = [1,2,3,4,5]
>>> tuple(a)
(1, 2, 3, 4, 5)
1
  • The OP wrote explicitly that this was not an option for him.
    – runlevel0
    Jan 30, 2018 at 13:57
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

1

tuple is not mutable in python.

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

a = [1,2,3,4,5]
tuple(a) 
1
  • But you can concatenate two tuples and reassign the result to the variable holding the tuple, so you can still incrementally build up a tuple, like in byron's or Josia's answers. The question is whether this is an efficient way to do it, though, since the interpreter will probably allocate new memory for each tuple you create, which it won't do if you operate on a list. Jun 21, 2016 at 16:48
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.