Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python list of tuples: increase number of tuple members

I have a list of tuples with the pattern "id", "text", "language" like this:

a = [('1', 'hello', 'en'), ...]

I would like to increase number of tuple members to "id", "text", "language", "color":

b = [('1', 'hello', 'en', 'red'), ...]

What is the correct way of doing this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you.

>Solution :

Since a tuple is immutable you have to create new tuples. I assume you want to add this additional value to every tuple in the list.

a = [('1', 'hello', 'en'), ('2', 'hi', 'en')]
color = 'red'

a = [(x + (color,)) for x in a]
print(a)

The result is [('1', 'hello', 'en', 'red'), ('2', 'hi', 'en', 'red')].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading