Concatenating Tuple
python
Solution
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.
Problem
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.