0

I'm new to python

I'm attempting to use the Hans volcano api to produce a map (https://volcanoes.usgs.gov/hans2/apiv2/volcanoApi/allWithNotice)

When printing my URL as json I am presented with many volcanoes in this format

[{'obsAbbr': 'avo', 'volcCd': 'ak6', 'volcName': 'Akutan', 'volcUrl': 'https://avo.alaska.edu/volcanoes/volcinfo.php?volcname=Akutan', 'vnum': '311320', 'imgUrl': 'https://avo.alaska.edu/images/dbimages/display/1108076476_60_3.jpg', 'threat': 'Very High Threat'},

From this I'm looking to extract volcano names. When making my dictionary I'm given one volcano only. I'm looking to obtain all the volcano names from this json file.

v = requests.get(url2.format()).json()

volcano = {
    'name' : v[0]['volcName'],
}

Any help would be appreciated, cheers

2
  • volcano = { 'name' : [d['volcName'] for d in v]}? You'll have to loop through v. Where you assign it depends on what format you want it in. Commented Oct 7, 2022 at 20:31
  • What is your desired output? A list of dicts where each have a name attribute? (like this [{'name':'Akutan'},{'name':'Aniakchak'},...]); or only 1 dict where the value for name is a list of names? (like this {'name':['Akutan','Aniakchak',...]}) Commented Oct 7, 2022 at 20:44

3 Answers 3

1

I wouldn't keep it as a list of dictionaries, just use a dictionary:

volcano_threats = {x['volcName']: x['threat'] for x in v}

Output:

{'Akutan': 'Very High Threat',
 'Alaskan Volcanoes': None,
 'Aniakchak': 'High Threat',
 'Atka volcanic complex': 'High Threat',
...
}

Or to keep all the data:

volcanos = {x['volcName']: x for x in v}
Sign up to request clarification or add additional context in comments.

Comments

1

You could do like this,

v = requests.get(url2.format()).json()
print([i['volcName'] for i in v])

Results

['Akutan', 'Alaskan Volcanoes', 'Aniakchak', 'Atka volcanic complex', 'Augustine', 'Bogoslof', 'Cleveland', 'Davidof', 'Dutton', 'Edgecumbe', 'Fourpeaked', 'Frosty', 'Gareloi', 'Great Sitkin', 'Griggs', 'Iliamna', 'Kanaga', 'Kasatochi', 'Katmai', 'Korovin', 'Little Sitkin', 'Mageik', 'Makushin', 'Martin', 'Novarupta', 'Okmok', 'Pavlof', 'Redoubt', 'Sanford', 'Semisopochnoi', 'Shishaldin', 'Snowy Mountain', 'Spurr', 'Takawangha', 'Tanaga', 'Trident', 'Ugashik-Peulik', 'Ukinrek Maars', 'Veniaminof', 'Westdahl', 'Wrangell', 'Coso Volcanic Field', 'Lassen Volcanic Center', 'Long Valley Caldera', 'Cascade Range', 'Mount Hood', 'Mount St. Helens', 'Newberry', 'Three Sisters', 'Haleakala', 'Hualalai', "Kama'ehuakanaloa", 'Kilauea', 'Mauna Kea', 'Mauna Loa', 'Ofu-Olosega', "Ta'u Island", 'Tutuila Island', 'Agrigan', 'Ahyi Seamount', 'Anatahan', 'Pagan', 'Sarigan', 'Yellowstone']

Edit

If you have more data to fetch,

[{'name':i['volcName'], 'threat':i['threat']} for i in v]

# result
[{'name': 'Akutan', 'threat': 'Very High Threat'},
 {'name': 'Alaskan Volcanoes', 'threat': None},
 {'name': 'Aniakchak', 'threat': 'High Threat'},
 {'name': 'Atka volcanic complex', 'threat': 'High Threat'},
 {'name': 'Augustine', 'threat': 'Very High Threat'},
....

1 Comment

Thanks, this works for obtaining all the names. How could I put this into a dictionary with another attribute such as 'threat'?
0

You can iterate over your data and generate a list with remapped dicts, all easily with a list comprehension syntax. Like this:

v = requests.get(url2.format()).json()
volcanoes = [dict(name=x['volcName'], threat=x['threat']) for x in v]

# [{'name': 'Akutan', 'threat': 'Very High Threat'},
#  {'name': 'Alaskan Volcanoes', 'threat': None},
#  {'name': 'Aniakchak', 'threat': 'High Threat'},
#  {'name': 'Atka volcanic complex', 'threat': 'High Threat'}, ...
# ]

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.