I have a simple Python class like this:
class P(object):
def __init__(self, x, y):
self.x = x
self.y = y
I create a list of these objects e.g.:
import random
list_of_objects = [P(random.random(), random.random()) for i in range(1000)]
How do I then unpack the attributes of P into different lists?
That is, my goal is to obtain list_of_x, list_of_y
, where these are defined as:
list_of_x = [p.x for p in list_of_objects]
list_of_y = [p.y for p in list_of_objects]
I was wondering if there was a cleaner way e.g. using zip/tuples, rather than the code above, which involves 2 for-loops.