You can simply loop through using a for loop.
Using for loop you can traverse through all the instances.
Here is a simple program that you can use after configuring your IAM using using AWS CLI.
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
print (instance.id , instance.state)
Hope this helps.