You build your colorMap as a large one-dimensional tuple, with the first index being int . This way your lambda is passed int , and then you try to index it.
You probably need a list of tuples:
colorMap = [] ... dist = dist_3d(colors[i], source) colorMap.append((dist, colors[i]))
As for the approach to sorting colors, I actually used the kdtree module for this, loaded with all RGB tuples. Then I can set the specified color tuple for the N nearest colors:
from kdtree import KDTree colors = [(10,10,10),(250,50,5),(100,50,20)] query_color = (175, 25, 50) tree = KDTree.construct_from_data(data)
source share