[Tutor] print issue

Alan Gauld alan.gauld at btinternet.com
Thu Oct 3 19:12:47 EDT 2019


On 03/10/2019 22:02, ose micah wrote:

> cloudfront_ips = [item['ip_prefix'] for item in ip_ranges if item ==
> "CLOUDFRONT_GLOBAL_IP_LIST"]

This line makes no sense.

First of all you treat item as a dictionary using item['ip_prefix']
Then you treat item as a string with item=="CLOUDFRONT_GLOBAL_IP_LIST"

It can't be both, its either a dict or a string. Which is it?



> ec2_ips = [item['ip_prefix'] for item in ip_ranges if item ==
> "CLOUDFRONT_REGIONAL_EDGE_IP_LIST"]

And this is the same.

> for ip in cloudfront_ips:
>     if ip not in ec2_ips:
>         cloudfront_ips_more_edge.append(ip)

Why not use list comprehensions here too:

cloudfront_ips_more_edge=[ip for ip in cloudfront_ips
                             if ip not in ec2_ips]

An alternative approach would be to use sets and
use the intersection and other set operators

> for ip in ec2_ips:
>     if ip not in cloudfront_ips:
>         cloudfront_ips_more_edge.append(ip)

same as above...

> for ip in cloudfront_ips_more_edge: print(str(ip))

I suspect ip is already a string so you shouldn't need the str() conversion.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list