Get class object

Lets say I have the following situation:

class myclass:
    def __init__ (self,myobject)

mylist=[myclass(i) for i in range(10)]

So I have a class that has one object, and I have a list of this class. How do I recall (not sure if this is the correct terminology) all the .myobject at once without putting them into a separate list? Obviously I can’t do mylist.myobject to get myobject as a list, and I can’t seem to do mylist[0:-1].myobject. What I want is to get [0,1,2,3,4,5,6,7,8,9] without having to do the original range(10) or put all .myobject into a list. My script is obviously more complicated than this example.

How about retrieving them with another list comprehension:
This is however just creating another list, but it’s retrieved from mylist.

class myclass:
    def __init__ (self,myobject):

mylist=[myclass(i) for i in range(10)]

myobjects= [cls.myobject for cls in mylist]

print myobjects

If this is not satisfactory, please elaborate on your needs as specific as possible.
Another approach would be to create a class that contains the myclasses, however inside you’d still need to generate a list form the myclasses stored inside


Thanks for the suggestion. The objective of getting these values is so that I can look at them all at once. Lets say .myobjects were a bunch of random numbers instead of a series, how do I sort the mylist so that the .myobjects are in ascending order?

I think I need a little background on what it is you are trying to do before I can give any suggestions on a solution.

I’m just thinking that lists have a lot of methods like sort() and index() and I was wondering how you can access the objects in a class as a list to use those methods. Lets say I have a class: person. Each person has a name, age, etc. as objects. How do I sort a list of people by their attributes/objects?

You can use the sorted function for generic sorting on any attribute.

But how do you turn the objects of a class into an iterable? Wouldn’t that require me to put the objects in a new list so that they are all in one list? Willem’s solution is the closest I think. Maybe there is not an elegant and simple way to do this except with loops. Going off my person example, how would I search for a person by hair color without a loop that checks each person’s hair object individually, since I won’t be able to use the index() method.

Say you have an object

class Person:
  def __init__(self, age):
    self.age = age

And a list of these objects

people = [Person(10), Person(4), Person(42), Person(12)]

you could then use the sorted function to sort your list.

ages = sorted(people, key=lambda person: person.age)

for person in ages:
    print person.age

Thanks for the explanation. Your solution certainly works, but I’m thinking that what I’m looking for is not possible. Using our example, what I wanted to do was people.age.sort() or people.age.index(42). Especially index(), where you look across all people to see who is 42. But I guess I could do:
oldpersonindex=[i for i in range(len(people)) if people[i].age==42]

One of the most common reasons to sort is so that you can quickly search for a specific item in the list. The sample that you provided will work just fine for 10 people, but when you have 100000 entries in your list searching will be magnitudes of speed faster when the list is sorted.

I don’t think I’m being very helpful on this topic, sorry.