Does it do what you want?
familiesNew=[ filter(lambda x:x!=i,j) for i,j in enumerate(families) ]
EDIT
Also note that the reason you failed is that in the third element of the external list ( [1, 2, 3, 4, 5] ) you are trying to get the fifth element in the for ( for j in families[i] == for j in [1,2,3,4,5] ), but the families [i] are 5, that is, the largest index is 4. Sorry if this explanation is a bit unclear ... maybe the following will help clean it:
families = [[0, 1, 2],[0, 1, 2, 3],[0, 1, 2, 3, 4],[1, 2, 3, 4, 5],[2, 3, 4, 5, 6]] def f(i,j): print i,j,families[i] return families[i][j] #THIS DOES NOT WORK -- but it will tell you where it failed. familiesNew = [ [ f(i,j) for j in families[i] if i !=j ] for i in range(len(families)) ]
mgilson
source share