dimanche 21 février 2021

Excluding Python Lists With Empty Values

I have two lists, if list has data, I want to create a variable and assign it a name. If not, I want to disregard it. I then want to combine the list names into one list.

I'm having difficulty returning an empty value if there is no data in the list.

countries = ['America', 'France']
cities = []

if len(countries) != 0:
    country_name = 'country'
else:
    country_name = None
if len(cities) != 0:
    city_name = 'city'
else:
    city_name = None

region_names = [country_name, city_name]

Getting:

['country', None]

Want:

['country']

Aucun commentaire:

Enregistrer un commentaire