Home > Mobile >  Creating <p> with each entry in a dictionary forloop
Creating <p> with each entry in a dictionary forloop

Time:10-09

I have a dictionary created of volcanoes from a USGS source

url3 = "https://volcview.wr.usgs.gov/vv-api/volcanoApi/wwvolcanoes"
vmap = requests.get(url3.format()).json()
volcanoinfo = [{
    'name':i['vn'], 
    'vlat':i['lat'],
    'vlng':i['lng'],
    'elev':i['elevM'],
    'obs':i['obsAbbr']} 
        for i in vmap]

From this I would like to create a

tag with the attribute vlat or vlng in each one, however when I use a for loop it it does not put anything within the tag. The result is the right number of

with no input.

 {%for i in volcanoinfo%}
  <p>{{ volcanoinfo['vlat'] }}</p>
  <p>{{ volcanoinfo['vlng'] }}</p>
{%endfor%}

Any help would be appreciated, cheers

CodePudding user response:

In the second code snippet you are trying to access 'vlat' from volcanoinfo, but volcanoinfo is a list, not a dictionary. Rather, every element in the volcanoinfo list is a dictionary object. So, in the second code snippet, use i['vlat'] instead of volcanoinfo['vlat'].

It might look something like bellow-

{%for i in volcanoinfo%}
 <p>{{ i['vlat'] }}</p>
 <p>{{ i['vlng'] }}</p>
{%endfor%}
  • Related