Lets say i have a list of tuples
tl = [("x","y"), ("y","z"), ("y","a")]
and for every tuple in the list I want to replace the occurrence of the first item in the tuple in the rest of the tuples except the tuple I'm currently working with so it would be something like this.
First iteration: This is the first tuple in the list: ("x","y")
Now I want to find if "x" appears in any of the other tuples except the tuple I'm currently working with. Since there aren't any other "x"'s the list will stay the same.
After:
tl = [("x","y"), ("y","z"), ("y","a")]
Second iteration: This is the second tuple in the list: ("y","z")
I want to find if "y" appears in any of the other tuples except the tuple I'm currently working with. In this case there is another "y" in the third tuple ("y","a") so we have to replace it with "z".
After: The third tuple was changed from ("y","a") to ("z","a")
tl = [("x","z"), ("y","z"), ("z","a")]
Third iteration: This is the third tuple in the list ("z","a").
I want to find if "z" appears in any of the other tuples except the one I'm currently working with and replace "z" with "a". In this case "z" appears in the first and second tuple.
After: The first tuple changed from ("x","z") to ("x","a"). The second tuple changed from ("y","z") to ("y","a").
tl = [("x","a"), ("y","a"), ("z","a")]
Since we already iterated all the items in the list we can stop and [("x","a"), ("y","a"), ("z","a")] is the result.
What would be the most efficient way to achieve this? and will it be easier to achieve it if I have a dictionary instead?