0

here a small Python (3.X) question:

consider having a list of objects from a class with several attributes.

Which would be a pythonic way to derrive the values for a specific attribute from all object instances as a list.

The following code is working, but I guess there might be more elegant ways?

class Point:
def __init__(self, x: float, y: float):
    self.x = x
    self.y = y

# create some test data
points = []
points.append(Point(10.0, 20.0))
points.append(Point(30.0, 10.0))
points.append(Point(10.0, 60.0))

#probably unpythonic way of derriving list of specific instance attribute data
x = []
for point in points:
    x.append(point.x)

#output
print("x=" + str(x))
# x=[10.0, 30.0, 10,0]

What would be your suggestion? Thanks!

Nisse
  • 43
  • 7

0 Answers0