1

I'm writing a script in Python, using the flickr.photos.search API to retrieve a JSON object containing ids and infos of photos with a specific tag.

response = flickr.photos.search(api_key = api_key, tags='painting')

This is what "response" looks like:

{'photos': {'page': 1, 'pages': 7112, 'perpage': 100, 'total': '711114', 'photo': [

{'id': '33675403798', 'owner': '93779577@N00', 'secret': 'cb33cbcde6', 'server': '7914', 'farm': 8, 'title': ' Door 5302AA', 'ispublic': 1, 'isfriend': 0, 'isfamily': 0}, 

{'id': '40586047293', 'owner': '93779577@N00', 'secret': 'd4a0df639e', 'server': '7849', 'farm': 8, 'title': 'Elevator Door 5302', 'ispublic': 1, 'isfriend': 0, 'isfamily': 0}, 

{'id': '33675168088', 'owner': '164939965@N03', 'secret': '8d271064e7', 'server': '7857', 'farm': 8, 'title': 'A New Shade Of Blue', 'ispublic': 1, 'isfriend': 0, 'isfamily': 0}, 

{'id': '32582314737', 'owner': '81035653@N00', 'secret': 'e78b4f7235', 'server': '7821', 'farm': 8, 'title': 'Saint Michael', 'ispublic': 1, 'isfriend': 0, 'isfamily': 0}, 

]}, 

'stat': 'ok'}

From this I would like to create Python list that contains only the "id"s of the photos, something that would look like this:

['33675403798', '40586047293', '33675168088', '32582314737']

How can I extract only the ids and create a list?




EDIT: Thank you to anyone who took the time to help me! I managed to create the list using this code:

tag="painting"
response = flickr.photos.search(api_key = api_key, tags=tag)

photo_list=[]
for record in response['photos']['photo']:
    s=record["id"]
    photo_list.append(s)

I hope it can help someone in the future.

0

3 Answers 3

3

Since flickr.photos.search already parses the JSON for you, you can access the photo list with response["photos"]["photo"].

Then, you can create a list containing only the ids you need with a list comprehension (a more Pythonic equivalent to a for loop to create a list):

photo_ids = [photo["id"] for photo in response["photos"]["photo"]]

The for loop equivalent for this code would be:

photo_ids = []
for photo in response["photos"]["photo"]:
    photo_ids.append(photo["id"])
Sign up to request clarification or add additional context in comments.

Comments

1

The flickr.photos.search() function returns you a dictionary containing other dictionaries. You are intereseted in the id's which are part of a dictionary (key: photos) which contains a list (key:photo) of dictionaries that have these values (key: id). You can extract your desired output with a list comprehension like that:

[photodict['id'] for photodict in response['photos']['photo']]

Comments

0

This should work:

Step - 1:

Set the dictionary to a variable, say df

df = {'photos': {'page': 1, 'pages': 7112, 'perpage': 100, 'total': '711114', 'photo': [

{'id': '33675403798', 'owner': '93779577@N00', 'secret': 'cb33cbcde6', 'server': '7914', 'farm': 8, 'title': ' Door 5302AA', 'ispublic': 1, 'isfriend': 0, 'isfamily': 0}, 

{'id': '40586047293', 'owner': '93779577@N00', 'secret': 'd4a0df639e', 'server': '7849', 'farm': 8, 'title': 'Elevator Door 5302', 'ispublic': 1, 'isfriend': 0, 'isfamily': 0}, 

{'id': '33675168088', 'owner': '164939965@N03', 'secret': '8d271064e7', 'server': '7857', 'farm': 8, 'title': 'A New Shade Of Blue', 'ispublic': 1, 'isfriend': 0, 'isfamily': 0}, 

{'id': '32582314737', 'owner': '81035653@N00', 'secret': 'e78b4f7235', 'server': '7821', 'farm': 8, 'title': 'Saint Michael', 'ispublic': 1, 'isfriend': 0, 'isfamily': 0}, 

]}, 

'stat': 'ok'}

Step-2:

id_list = [df['photos']['photo'][i]['id'] for i in range(len(df['photos']['photo']))]

Output: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.