A simple approach would be to assign an empty list to each unique element in the tuple. Then append the index of each element in the corresponding list.
Here is my code:-
I am creating an empty dictionary.
tup = ('A', 'A', 'B', 'B', 'A')
d = {}
I am assigning an empty list in the dictionary to each unique element in the tuple.
for i in range(len(tup)):
d[tup[i]] = []
Appending the index of each element of the tuple in its corresponding list in the dictionary
for j in range(len(tup)):
d[tup[j]].append(j)
The complete code:-
tup = ('A', 'A', 'B', 'B', 'A')
d = {}
for i in range(len(tup)):
d[tup[i]] = []
for j in range(len(tup)):
d[tup[j]].append(j)
print(d)