Home > Back-end >  Is there a way to combine the following two list comprehensions?
Is there a way to combine the following two list comprehensions?

Time:09-08

I have a list of "point_objects", from which I build locx and locy. Please see below. Can these two list comprehensions be brought to just a single line?

self.locx = [pobj.x for pobj in point_objects]
self.locy = [pobj.y for pobj in point_objects]

CodePudding user response:

From the title, I understood that you want one line that computes both of these. You can use zip():

self.locx, self.locy = zip(*((pobj.x, pobj.y) for pobj in point_objects))
  • Related