I have a particular problem that has me stumped. Suppose I have the following two lists:
x = ["A","B","C","D","E"]
y = [1,2,3,2,1]
x
and y
have a relationship. The relationship is tied by index. That is, "A" relates to 1, "B" related to 2, "C" related to 3 and so on.
What I am trying to do is create a key value relation where the unique items in y
are keys and each key has a list that contains the letters related to the key as mentioned previously. I attempted to do the following:
mapping = dict(zip(y,x))
{1: 'E', 2: 'D', 3: 'C'}
This overwrites the previous letter. I would love to be able to return the following:
{1:['A','E'], 2:['B','D'], 3:['C']}
Anyone have a clever solution to this? Preferably without itertools.