Skip to content
Advertisement

Is there a way to combine the following two list comprehensions?

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]

Advertisement

Answer

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))
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement