I m Using python countries library and want to list and print total states and cities of given country using python is it possible to do it

Viewed 30
def Contries(request):
    for country in pycountry.countries:
        list.append(country.name)
    if list:
        return JsonResponse(
            {"code": status.HTTP_200_OK, "success": True, "response": list},
            status=status.HTTP_200_OK)
    else:
        return JsonResponse(
            {"code": status.HTTP_400_BAD_REQUEST, "success": False, "response":"list"},
            status=status.HTTP_400_BAD_REQUEST)

Is there any possibility to get the list of cities and states of given country using python

1 Answers

This is an example of how you can use pycountry:

import pycountry

def cities_in_country(country):
    list_ = []
    for region in pycountry.subdivisions.get(country_code=country):
        list_.append(region.name)
    return list_


print(cities_in_country("FR"))
Related