1

I have a list full of classes of the same type:

class Example():

    def __init__(self, myVar):
        self.myVar = myVar
        # other variables and functions
        # ...
  

Following example shows the list:

myList = []
for i in range(0,10):
    myList.append(Example(i))

And now i want a quick way to get a new list with all myVar of all Example-classes. I thoght of something like:

myVector = myList[:].myVar

But this doesn't work. Can anyone help me?

Pendrois
  • 13
  • 2
  • You need to loop over `myList`. lists don't support vectorized operations. You could use a list comprehension as well. – juanpa.arrivillaga Feb 10 '21 at 17:23
  • 1
    OP, why would that work? `myList[:]` is a list object that doesn't contain a `myVar` attribute. Loop over `myList` and take the `.myVar` for each element: `[e.myVar for e in myList]` – Pranav Hosangadi Feb 10 '21 at 17:23

0 Answers0