-2

How to add value to tuple within list

a = [[('one','1'),('two','2')],[('three','3')]]

b = [['I','II'],['III']]

I want it to turn out like

a = [[('one','1','I'),('two','2','II')],[('three','3','III')]]

I try to use append but it doesn't work Thanks for any help.

  • I used your question to search here on SO and this is the first it found after yours: https://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples/626871#626871 – Ovski Oct 18 '22 at 09:25

1 Answers1

1

Tuples are immutable, which means you can't change them once they've been made. See here: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences.

The only way to achieve what you want would be to either remake your list, or delete the elements and add the new versions in-place.

You could try something like this:

a = [(1, 'one'), (2, 'two'), (3, 'three')]
b = ["I", "II", "III"]

updated_a = [(*i, j) for i, j in zip(a, b)]

Where I've assumed you've typoed in your question, and that the list of numerals has this form instead.

whatf0xx
  • 108
  • 7