0

I have a simple class Foo given by:

class Foo:
    def __init__(self, bar: list):
        self.bar = bar

and an array of instances of this class foos given by:

foos = []
foos.append(Foo(bar=[1, 2]))
foos.append(Foo(bar=[3, 4]))
foos.append(Foo(bar=[5, 6]))

I want to get an array of bars such that:

bars = [[1, 2], [3, 4], [5, 6]]

How do I go about this please?

ajrlewis
  • 2,968
  • 3
  • 33
  • 67

1 Answers1

2

You can use a simple list comprehension:

bars = [foo.bar for foo in foos]
mfrackowiak
  • 1,294
  • 8
  • 11