Python list index out of range in for loop

Question:

With the aviationstack API, I try to get info about every flight departing from Marseille (France) airport.

I use the first steps to determine how many pages I have to iterate, as the API returns the total number of results and documentation says the default response has a limit of 100 results, then I build URL’s according to the latter result.

Though the code below could be simplified, it gives promising results, however at the last step, I need to provide counts at the line flight_data = response.get("data") in order to get every item of each offset.

But it returns IndexError: list index out of range. I guess because the last offset contains less than 100 items.

import requests
import json
import pandas as pd

from pprint import pprint

airport = 'LFML' # ICAO code for Marseille Provence airport, France
KEY = XXX

urllst = []
responselist = []
countlst = []
data = []

# determine how many results we get
urlroot = f'http://api.aviationstack.com/v1/flights?access_key={KEY}&dep_icao={airport}'
r = requests.get(urlroot)
resp = r.json()
total_results = resp.get("pagination").get("total")
pprint(f'there are {total_results} results in total')

# the total number of results gives us the offset number
offsetnumber = round(total_results, -2)
pprint(f'offset number for {airport} is then {offsetnumber}')

# build URL's according to the offset numbers we obtained
for i in range(0, offsetnumber + 100, 100):
  urls = 'http://api.aviationstack.com/v1/flights?access_key='+KEY+'&limit=100&offset='+str(i)+'&dep_icao='+airport
  urllst.append(urls)

# call the API and store the responses in a list
for url in urllst:
  req = requests.get(url)
  responses = req.json()
  responselist.append(responses)

# get the count of results by offset
for response in responselist:
  counts = response.get("pagination").get("count")
  offset = response.get("pagination").get("offset")
  pprint(f'there are {counts} results in offset {offset}')
  countlst.append(counts)
  
  flight_data = response.get("data")[counts`]
  flight = flight_data.get('flight').get('icao')
  #print(f"{flight}")
  #print(flight)
  data.append((flight))
  cols=['flights']

result = pd.DataFrame(data, columns=cols)
result
Asked By: hug

||

Answers:

Replace this:

    flight_data = response.get("data")[0]
    flight = flight_data.get('flight').get('icao')
    data.append((flight))

with this:

    data.extend(
        flight_data.get('flight').get('icao')
        for flight_data in response.get('data')
    )

to append each flight element to data instead of just the first one. If response.get('data') is empty, this will be an empty sequence so nothing will get appended (instead of raising an error).

Answered By: Samwise
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.