How to add value to a tuple?

I am working on a script where I have a list of tuples of type ('1','2','3','4') . eg:.

 list = [('1','2','3','4'), ('2','3','4','5'), ('3','4','5','6'), ('4','5','6','7')] 

Now I need to add '1234' , '2345' , '3456' and '4567' respectively at the end of each tuple. eg:

 list = [('1','2','3','4','1234'), ('2','3','4','5','2345'), ('3','4','5','6','3456'), ('4','5','6','7','4567')] 

Is it possible?

+42
python tuples
Feb 06 '11 at 12:50
source share
6 answers

Tuples are immutable and should not be changed - the list type is used for this. You can replace each tuple with originalTuple + (newElement,) , thus creating a new tuple. For example:

 t = (1,2,3) t = t + (1,) print t (1,2,3,1) 

But I would prefer to go with lists from the very beginning, because they insert elements faster.

And one more hint: do not overwrite the built-in name list in your program, rather call the variable l or another name. If you overwrite the built-in name, you can no longer use it in the current area.

+74
Feb 06 2018-11-11T00:
source share

Based on the syntax, I assume this is Python. The tuple's point is that it is immutable, so you need to replace each element with a new tuple:

 list = [l + (''.join(l),) for l in list] # output: [('1', '2', '3', '4', '1234'), ('2', '3', '4', '5', '2345'), ('3', '4', '5', '6', '3456'), ('4', '5', '6', '7', '4567')] 
+11
06 Feb 2018-11-12T00:
source share

In Python you cannot. Tuples are immutable.

In the containing list, you can replace tuple ('1', '2', '3', '4') with another tuple ('1', '2', '3', '4', '1234') .

+8
Feb 06 '11 at 12:55
source share

Like other people, tuples in python are immutable, and the only way to โ€œchangeโ€ one is to create a new one with the added elements enabled.

But the best solution is a list. If you need to call any function or method that requires a tuple, create a tuple using the tuple (list).

+5
Feb 08 '11 at 18:48
source share

I was looking through some details related to tuple and list , and I realized:

  • Tuples are a Heterogeneous collection data type
  • Tuple has a fixed length (for each type)
  • The tuple is always final

So, to add a new element to the tuple, you need to drop it on the list and perform the append() operation, and then return it to the tuple again.

But personally, I thought that if Tuples should be finite , fixed in length, and if we use these data types in our application logic, then there should not be a script for adding new elements or updating the value of an element in it. So instead of a list of tuples, it should be a list of lists , am I right on that?

+3
Jul 14 '16 at 6:06
source share
  list_of_tuples = [('1', '2', '3', '4'), ('2', '3', '4', '5'), ('3', '4', '5', '6'), ('4', '5', '6', '7')] def mod_tuples(list_of_tuples): for i in range(0, len(list_of_tuples)): addition = '' for x in list_of_tuples[i]: addition = addition + x list_of_tuples[i] = list_of_tuples[i] + (addition,) return list_of_tuples # check: print mod_tuples(list_of_tuples) 
+2
Jun 18 '13 at 3:05
source share



All Articles