def parse_multiple_countries(data_list): """ @return: list of countries """ #get_country_id = lambda item : item['country']['id'] # When it's a list of indicator the World bank returns ISO2 codes labeled 'id' get_country_id_iso2 = lambda item : item['country']['id'] countries = [] #data_list.sort(key = get_country_id) while len(data_list)>0: current_id_iso2 = get_country_id_iso2(data_list[0]) country_data = filter(lambda item : get_country_id_iso2(item) == current_id_iso2, data_list) # we now basically don't know the country's "real" (iso3) id country = Country("") country.code_iso2=current_id_iso2 indicator = parse_single_country(country_data) country.set_indicator(indicator) countries.append(country) # remove parsed data from the list data_list = [item for item in data_list if item not in country_data] return countries