[Tutor] print issue

ose micah osaosemwe at yahoo.com
Fri Oct 4 10:30:28 EDT 2019


 Hello Alan, 
Thanks, It is a dictionary.
I wrote it that way because, am trying to append the list of ips in both the cloudfront_ips and the ec2_ips as one:
Here is the updates:

#!/usr/bin/env python
import requestsimport sysimport fileinput
ip_ranges = requests.get('http://d7uri8nf7uskq.cloudfront.net/tools/list-cloudfront-ips').json()cloudfront_ips = [item['CLOUDFRONT_GLOBAL_IP_LIST'] for item in ip_ranges]ec2_ips = [item['CLOUDFRONT_REGIONAL_EDGE_IP_LIST'] for item in ip_ranges]
for ip in cloudfront_ips    if ip not in ec2_ips        cloudfront_ips_more_edge.append(ip)
for ip in ec2_ips    if ip not in cloudfront_ips        cloudfront_ips_more_edge.append(ip)

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


such that output would be like
52.94.22.0/2452.94.17.0/2452.95.154.0/2352.95.212.0/2254.239.0.240/2854.239.54.0/2352.119.224.0/21....
    On Thursday, October 3, 2019, 07:12:49 PM EDT, Alan Gauld <alan.gauld at btinternet.com> wrote:  
 
 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