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.
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