I'm building a similarity matrix of a list of items. The naive approach is to iterate the list twice, but this needlessly will compare A:B and B:A when they're the same.
for A in items:
for B in items:
if A==B: continue
sim[A][B] = calc_sim(A, B)
is there a simple way to only calculate half of the values? I could put a skip in there like
if sim[B][A]: continue # already calculated in other direction
But still the iteration is happening. Effectively I just want to iterate through the top or bottom half of the grid:
There are some similar Qs, but nothing with a canonical answer. This seems like a basic CS algo question!