0

I need to delete a list from a list of lists based off a second list. I have the following:

LA = []
LA.append([u'02fc0b', u'val1', u'val2'])
LA.append([u'0a1ac', u'val1', u'val2'])
LA.append([u'02fc0b', u'val1', u'val2'])
LA.append([u'safsdf', u'val1', u'val2'])
LA.append([u'lmuylj', u'val1', u'val2'])


EXCL = []
EXCL.append('02fc0b')
EXCL.append('safsdf')

And I'd like to exclude any list in LA[] where the value in position 0 appears in EXCL[]. I can totally do this with a loop, but I feel like there is a more Pythonic approach to use, and I'd love to learn.

3
  • 2
    result = [x for x in LA if x[0] not in EXCL] Commented May 22, 2017 at 19:13
  • A loop is totally pythonic Commented May 22, 2017 at 19:24
  • can you help me define pythonic? Commented May 22, 2017 at 19:44

1 Answer 1

4

you could use filter with custom condition:

LA = []
LA.append([u'02fc0b', u'val1', u'val2'])
LA.append([u'0a1ac', u'val1', u'val2'])
LA.append([u'02fc0b', u'val1', u'val2'])
LA.append([u'safsdf', u'val1', u'val2'])
LA.append([u'lmuylj', u'val1', u'val2'])


EXCL = []
EXCL.append('02fc0b')
EXCL.append('safsdf')

d = set(EXCL)
print(list(filter(lambda v: v[0] not in d, LA)))
#[['0a1ac', 'val1', 'val2'], ['lmuylj', 'val1', 'val2']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.