0

I am trying to use list comprehension to filter this list:

inp = [['Fire 46.0.1', 'vlc 2.2.3','opt 0.9.9.10'],
       ['opt 0.9.9.11', 'notepad 6.9', 'adobe. 6.9', 'vlc 2.3.3']]

into

out = [['Fire 46.0.1', 'vlc 2.2.3',],
       ['notepad 6.9', 'adobe. 6.9', 'vlc 2.3.3']]

i.e. removing all the strings that start with 'opt'. I am trying something like

[soft for ls in inp if soft not soft.startswith('opt')]

But something is wrong with the syntax. Any help?

2 Answers 2

3

This should do:

[[s for s in item if not s.startswith('opt')] for item in inp ]

Use startswith to filter out items with opt from the sublists

Sign up to request clarification or add additional context in comments.

Comments

2

You need a nested list comprehension for filtering a nested list.

[[y for y in x if not y.startswith('opt')] for x in inp]

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.