I am trying to create a Graph of nodes as a Dictionary but I'm getting some unexpected results:
It is a Node of the Graph if the word's last letter is equal of the second name first letter
My list:
names = ["Mark", "Kelly", "Kurt", "Terk"]
My code:
n = [ x.lower() for x in names ]
graph = {}
temp = []
for x in n:
temp.clear()
for y in n:
if(x[-1]==y[0] and not x==y):
temp.append(y)
graph[x] = temp
Result:
{'kurt': ['kelly', 'kurt'], 'terk': ['kelly', 'kurt'], 'kelly': ['kelly', 'kurt'], 'mark': ['kelly', 'kurt']}
Expected behavior
{'kurt': ['terk'], 'terk': ['kelly', 'kurt'], 'kelly': [], 'mark': ['kelly', 'kurt']}
What am I doing wrong?