0

I need to remove the sub lists from a list in Python.

For example: The main list A contains

A=[ 'a,b,c', 'd,e,f' , 'g,h,i' , 'g,l,m' , 's,l,k' , 'd,k,l', 'a,g,d' ]

I need to remove the sub lists from A which begin with the items in the following list:

B = ['g','d']

so that Final List A = [ 'a,b,c', 's,l,k' , 'a,g,d' ]

Thanks in advance

6

2 Answers 2

2

Using list comprehension:

print([x for x in A if x[0] not in ['g', 'd']])
Sign up to request clarification or add additional context in comments.

2 Comments

@CeliusStingher how does it not?
Sorry, my bad. It does work I was having the wrong input. Changed to +1
0

You can do it using list comprehension and split(",").

print([e for e in A if e.split(",")[0] not in B])

Output

['a,b,c', 's,l,k', 'a,g,d']

Your output above for your approach is wrong. 2nd Element 'd,e,f' should also be removed as start element 'd' is in second list.

1 Comment

@P_Ar, glad that it helped.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.