List comprehension to access a nested list within a list only in one index

What I need is to access an index of a nested list to filter the elements from another index in a list of tuples…
For instance I have the following sample data:

samples = [('waterfall03', 9)] 

distances = [('waterfall03', [12.03400784892136, 13.17207261956732, 13.91981431878607, 13.095577523116825, 13.405988675235129, 13.144871284931902, 13.597874214619402, 12.731745772059295, 13.90083238519232, 0.0])]

So in this case I have the sample waterfall03 where I have the number 9, which is the index to be removed from the index number 1 from the list of tuples in the list distances… i.e. I want to get a list as follows [12.03400784892136, 13.17207261956732, 13.91981431878607, 13.095577523116825, 13.405988675235129, 13.144871284931902, 13.597874214619402, 12.731745772059295, 13.90083238519232]

I have written the following code which is not working

distances = [[n for j, n, m in enumerate(zip(subarray, samples)) if j != m] for i, subarray in enumerate(distances) if i == 1]

Expected output

distances = [('waterfall03', [12.03400784892136, 13.17207261956732, 13.91981431878607, 13.095577523116825, 13.405988675235129, 13.144871284931902, 13.597874214619402, 12.731745772059295, 13.90083238519232])]

More test cases

Samples [('highway_bost321', 0), ('mountain_0871', 2), ('opencountry_043', 4), ('opencountry_053', 5), ('opencountry_123', 6), ('palace_019', 7), ('volcano_010', 8), ('waterfall03', 9)]



 distances =  [('highway_bost321', [0.0, 10.174343253183386, 10.947746706490813, 7.187637514988234, 7.660483562939873, 10.622402335214636, 10.737785768990813, 10.566917715980832, 10.819389772897063, 12.03400784892136]), ('mountain_0871', [10.947746706490813, 0.83758544921875, 0.0, 5.838234191502578, 5.363256367154217, 1.3175048828125, 3.0810546875, 6.634500456993904, 0.78460693359375, 13.91981431878607]), ('opencountry_043', [7.660483562939873, 4.668136048210366, 5.363256367154217, 2.8142531243329643, 0.0, 5.347678752303554, 3.236628762987552, 5.377074226549635, 5.634096535101808, 13.405988675235129]), ('opencountry_053', [10.622402335214636, 0.73712158203125, 1.3175048828125, 5.876377140065916, 5.347678752303554, 0.0, 3.1134033203125, 6.11476575647307, 1.97711181640625, 13.144871284931902]), ('opencountry_123', [10.737785768990813, 3.01690673828125, 3.0810546875, 5.560564920669245, 3.236628762987552, 3.1134033203125, 0.0, 5.71620618616057, 3.09417724609375, 13.597874214619402]), ('palace_019', [10.566917715980832, 5.890665007775154, 6.634500456993904, 3.7392389463104037, 5.377074226549635, 6.11476575647307, 5.71620618616057, 0.0, 6.493675414476205, 12.731745772059295]), ('volcano_010', [10.819389772897063, 1.294677734375, 0.78460693359375, 5.709877257908828, 5.634096535101808, 1.97711181640625, 3.09417724609375, 6.493675414476205, 0.0, 13.90083238519232]), ('waterfall03', [12.03400784892136, 13.17207261956732, 13.91981431878607, 13.095577523116825, 13.405988675235129, 13.144871284931902, 13.597874214619402, 12.731745772059295, 13.90083238519232, 0.0])]

Expected outputs

       distances =  [('highway_bost321', [10.174343253183386, 10.947746706490813, 7.187637514988234, 7.660483562939873, 10.622402335214636, 10.737785768990813, 10.566917715980832, 10.819389772897063, 12.03400784892136]), 
    ('mountain_0871', [10.947746706490813, 0.83758544921875, 5.838234191502578, 5.363256367154217, 1.3175048828125, 3.0810546875, 6.634500456993904, 0.78460693359375, 13.91981431878607]), 
    ('opencountry_043', [7.660483562939873, 4.668136048210366, 5.363256367154217, 2.8142531243329643, 5.347678752303554, 3.236628762987552, 5.377074226549635, 5.634096535101808, 13.405988675235129]), 
    ('opencountry_053', [10.622402335214636, 0.73712158203125, 1.3175048828125, 5.876377140065916, 5.347678752303554, 3.1134033203125, 6.11476575647307, 1.97711181640625, 13.144871284931902]),
    ('opencountry_123', [10.737785768990813, 3.01690673828125, 3.0810546875, 5.560564920669245, 3.236628762987552, 3.1134033203125, 5.71620618616057, 3.09417724609375, 13.597874214619402]), 
    ('palace_019', [10.566917715980832, 5.890665007775154, 6.634500456993904, 3.7392389463104037, 5.377074226549635, 6.11476575647307, 5.71620618616057, 6.493675414476205, 12.731745772059295]), 
    ('volcano_010', [10.819389772897063, 1.294677734375, 0.78460693359375, 5.709877257908828, 5.634096535101808, 1.97711181640625, 3.09417724609375, 6.493675414476205, 13.90083238519232]),
    ('waterfall03', [12.03400784892136, 13.17207261956732, 13.91981431878607, 13.095577523116825, 13.405988675235129, 13.144871284931902, 13.597874214619402, 12.731745772059295, 13.90083238519232])]

Source: Python Questions

LEAVE A COMMENT