Saturday, June 6, 2009

Easier, Faster Property Enumeration in Python

I just discovered a really neat trick in Python to take a collection of objects, and turn one of their properties into a list. It's not terribly difficult to perform this the old way...

subscribers = []
for subscription in self.subscriptions.all():
subscribers.append(subscription.user)
return subscribers
This would return something like [(User:bob),(User:jerry),(User:tim)] and so on. However, this can be done in just a single line...
return [s.user for s in self.subscriptions.all()]
+1 for Python coolness.

0 comments:

  © Blogger template 'Minimalist G' by Ourblogtemplates.com 2008

Back to TOP