-1

Say I have a class called 'points' with attributes 'x' and 'y'

and I create a list of points p = [p1,p2,p3,p4] where p1.x = x1, py.y = y1 ...

How do I easily extract all the x and y values as a list? e.g.

x = [x1,x2,x3,x4]
y = [y1,y2,y3,y4]

can this be done simply in one line of code in python?

T-RexMan
  • 13
  • 4
  • Welcome to StackOverflow, please, take the [tour](https://stackoverflow.com/tour) and read [this guide](https://stackoverflow.com/help/how-to-ask) to ask a good question.Here, you could have provided us the code of the point class, and the code to create the list of points, so we could help you easily – totok Aug 17 '20 at 09:43
  • 1
    Does this answer your question? [How to extract from a list of objects a list of specific attribute?](https://stackoverflow.com/questions/677656/how-to-extract-from-a-list-of-objects-a-list-of-specific-attribute) – tevemadar Aug 17 '20 at 10:05

4 Answers4

2

You can do something like this:

p = [p1,p2,p3,p4]
x = [point.x for point in p]
y = [point.y for point in p]
rémi couturier
  • 425
  • 3
  • 11
1
x = [point.x for point in p]
y = [point.y for point in p]

could work but this will iterate the p twice, which is unnecessary ,You could try:

p = [p1, p2, p3, p4]
x, y = list(zip(*[(point.x, point.y) for point in p]))
Kevin Mayo
  • 1,089
  • 6
  • 19
0

Here you are

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

points = [Point(1,2), Point(3,4), Point(5,6), Point(7,8)]

x = [p.x for p in points]
y = [p.y for p in points]

print(x)
print(y)

Output :

[1, 3, 5, 7]
[2, 4, 6, 8]

Try it here

totok
  • 1,436
  • 9
  • 28
-1

You could do it like this

x, y = [point.x for point in p],[point.y for point in p]
Marko Borković
  • 1,884
  • 1
  • 7
  • 22
  • Is this one line? – Asocia Aug 17 '20 at 09:51
  • @Asocia yes it is – Marko Borković Aug 17 '20 at 09:53
  • You are constructing 2 for loops which is no different than [rémi couturier's answer](https://stackoverflow.com/a/63448327/9608759). For *real* one line solution, check [Kevin Mayo's answer](https://stackoverflow.com/a/63448396/9608759). – Asocia Aug 17 '20 at 09:56
  • What do you mean real one line solution dude? The code is in one line. There is no specification how many loops can be in one line. – Marko Borković Aug 17 '20 at 09:56
  • A one line solution should be easy enough to read, understand and maintain. You are just writing 2 for loops in one line and if OP need to add another dimension (say `z`), they need to add a whole new loop which is not Pythonic. – Asocia Aug 17 '20 at 10:02
  • @Asocia I have done some testing and it would appear my solution is faster. I could have messed up in the testing tho. https://repl.it/@BufferGigachad/BenchmarkTest#main.py btw I know the testing code is really ugly but I just quickly put it together. – Marko Borković Aug 17 '20 at 10:30
  • Your testing seems correct but https://stackoverflow.com/q/183201/9608759. OP doesn't mention that they have millions of `Point`s. – Asocia Aug 17 '20 at 10:33