jopo lona
jopo lona

Reputation: 11

Append difference of elements in tuples in list

If I have a list such as [(10, 22), (12, 50), (13, 15)] and would like to append the difference of these numbers so that the list would look like [(12, 10, 22), (38, 12, 50), (2, 13, 15)] how can I do this?

I have this line of code newList = [[???]+list(tup) for tup in list] but am not sure what to put where the question marks are to get what I want.

Thanks a lot

Upvotes: 1

Views: 29

Answers (1)

Back2Basics
Back2Basics

Reputation: 7806

tuples can't be modified (they are immutable). So you will have to create new tuples. It looks like you are prepending the difference rather than appending.

newList = [(b-a, a,b) for (a,b) in oldList] 

Upvotes: 3

Related Questions