Skip to content

Instantly share code, notes, and snippets.

@garnaat
Created September 25, 2012 23:39
Show Gist options
  • Save garnaat/3785118 to your computer and use it in GitHub Desktop.
Save garnaat/3785118 to your computer and use it in GitHub Desktop.
Most concise way to extract list of Instances from a list of Reservations in boto
#
# EC2 returns a list of Reservation objects when you call DescribeInstances.
# Usually, you just want the Instance objects that are embedded in the
# Reservation object. This shows how to use the magic of nested list
# comprehensions to construct a one-liner to get the Instance objects.
>>> import boto
>>> ec2 = boto.connect_ec2()
>>> reservations = ec2.get_all_instances()
>>> instances = [i for r in reservations for i in r.instances]
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment