Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using identical list comprehensions on two separate lists

Tags:

python

This feels like a simple question, but I haven't been able to figure it out and I haven't been able to find the answer anywhere.

In one line, how can I use two identical list comprehensions on two different lists, and return the results as two seperate lists?

I am trying to do this:

listx = [x for x in listx if x != None]
listy = [y for y in listy if y != None]

Is there a way to do this in one line? Perhaps using map() or a list comprehension?

like image 653
asheets Avatar asked Dec 08 '22 16:12

asheets


1 Answers

Though I don't get why it has to be a one-liner, this should do what you want:

listx, listy = [[x for x in alist if x != None] for alist in [listx, listy]]
like image 158
Mr. T Avatar answered Feb 17 '23 06:02

Mr. T