bolek
bolek

Reputation: 7

specified list comprehensions

Let say I've got two 1D list

firstList = [ "sample01", None, "sample02", "sample03", None ]
secondList = [ "sample01", "sample02", "sample03", None, None, None, "sample04"]

Now I'm looking for recipe for listComprehension that will return firstList,secondList but without None objects.

So it should look like this

listComprehension_List = [  [ "sample01","sample02","sample03" ] ,  [ "sample01","sample02","sample03", "sample04"  ]     ]
listComprehension_List = [[firstList without NONE objects],[secondList without NONE objects]]

I'm looking forward for any input ... now I will continue to try!

Upvotes: 1

Views: 69

Answers (1)

jamylak
jamylak

Reputation: 133744

>>> firstList = [ "sample01", None, "sample02", "sample03", None ]
>>> secondList = [ "sample01", "sample02", "sample03", None, None, None, "sample04"]

With a list comp

>>> [x for x in firstList if x is not None]
['sample01', 'sample02', 'sample03']

or you can just use filter

>>> filter(None, secondList)
['sample01', 'sample02', 'sample03', 'sample04']

For both:

>>> [[y for y in x if y is not None] for x in (firstList, secondList)]
[['sample01', 'sample02', 'sample03'], ['sample01', 'sample02', 'sample03', 'sample04']]

Upvotes: 5

Related Questions