Home > Blockchain >  deleting list from list of lists
deleting list from list of lists

Time:04-30

I need to delete these lists inside of list that contains the '/' symbol.

list for example:

X = [['a/','$1'], ["c","d"]]

so X[0] should be deleted. the actual list are much longer and contains more instances of this condition.

I tried use something like:

print([l for l in X if l.count("/") <1])

but if I understand correctly because the '/' is attached to another symbol he is not counted. Should I convert this list of lists to string, separate the '/' from another character, and then use the count function, or there is better solution?

thank you.

CodePudding user response:

One way to search "/" in each item in the sublists is to wrap a generator expression with any. Since you don't want sublists with "/" in it, the condition should be not any():

out = [lst for lst in X if not any('/' in x for x in lst)]

Output:

[['c', 'd']]

CodePudding user response:

The call to filter() applies that lambda function to every list in X and filters out list with '/'.

result = list(filter(lambda l: not any('/' in s for s in l), X))
  • Related