# Why we use TUPLES?

I was just reviewing extended Euclidean algorithm and I found use of tuples in iterative approach. I know about tuples but still I am not sure why in extended Euclidean algorithm did tuples were used ? how it helped ?
And in which case We should Consider Using Tuples ?

2 Likes

Show the code

1 Like

here it is on CP-algorithms
Extended Euclidean Algorithm - Competitive Programming Algorithms (cp-algorithms.com)

to pack and unpack. simple. if you want to do it without using tuples u need to create temporary variables

lets take an example

Suppose you are writing a function for fibonacci number using iterative version

suppose prev number is b and previous prev number is a

then currently you have a, b
next will be a, b, a+b
observe that new a = b
and new b = a+b

if you simply do a = b; b = a+b;
then it will be wrong

same for b=a+b; a=b;

since b or a changes first;

you can use a temp variable like this to achieve correctness

temp = b
b = a+b
a = temp

now this is correct new a and b
The problem is that one of the variable changes before second assignment. If you could first do calculation and then assign to them simultaneously it wouldn’t be a problem

like
a, b = b, a+b
since right hand is evaluated first and then pushed simultaneously on the left

I hope you got this
use tuples or temp
tuples is a bit clean. Also other ways can be to use pairs. but then you need to right pair.first, pair.second

so simply use tuples which behave like python unpacking but with weird long syntax and names

3 Likes

Thank you for help…