0

here my two tuples :

tuple1 = (320,240)
tuple2 = (5,5)

I would like to add this two element like an addition like that :

finalTuple = (325, 245)

I know this question has been already asked but I didn't found my answer. Thank you.

3 Answers3

1

You cannot add tuples directly, you can use a a comprehension to add the tuples:

finalTuple = tuple(t1 + t2 for t1, t2 in zip(tuple1, tuple2))

If you want to add them like that: finalTuple = tuple1 + tuple2, you should convert them to numpy array, where the addition operator is defined like that.

tuple1 = np.array(tuple1)
tuple2 = np.array(tuple2)
finalTuple = tuple1 + tuple2

And you can convert it back to a tuple with:

finalTuple = tuple(finalTuple)
czr
  • 658
  • 3
  • 13
0
final_tuple = tuple((i+j) for i,j in zip(tuple1, tuple2))
curlycharcoal
  • 191
  • 1
  • 9
0

You can use

final_tuple = tuple(v + tuple2[i] for i, v in enumerate(tuple1))
Leo Arad
  • 4,452
  • 2
  • 6
  • 17