Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find if a 2D list contains a value where the 3rd column is not relevant

I have a nested list where I’m only interested in the first 2 columns. (e.g., the first 2 columns are coordinates and the 3rd is the f value from A*, or some id for the coordinate, etc.)

a = [[1, 0, 2],
     [1, 1, 2],
     [1, 2, 2]]

I have an variable b = [1, 0] and I want to know if it is in the list, but I don’t care what the 3rd column contains, so I am expecting the output to be True

Is it possible to do something like this without using loops? e.g.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if b in a[:][:2]:
    do stuff

Or maybe [b[0], b[1], *] in a where * is a wildcard? Neither of these work, they are just to try and explain what I’m getting at.

Obviously if I naïvely test b in a I get False because of the 3rd column.

>Solution :

Using map to remove the third element for each list in a –

if b in list(map(lambda x: x[:2], a)):
    # do something
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading