Test/Python(20220101~)

Days9_리스트 속 딕셔너리에 데이터 추가

kiostory 2022. 1. 13. 22:06
travel_log = [
{
  "country": "France",
  "visits": 12,
  "cities": ["Paris", "Lille", "Dijon"]
},
{
  "country": "Germany",
  "visits": 5,
  "cities": ["Berlin", "Hamburg", "Stuttgart"]
},
]
#? Do NOT change the code above
#TODO: Write the function that will allow new countries
#to be added to the travel_log. ?
def add_new_country(country, howmanytimes, cities):
    travel_log.append(
        {
            "country": country,
            "visits": howmanytimes,
            "cities": cities,
        }
    )
#? Do not change the code below
add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])
print(travel_log)
-------------------------------------------------------------------------
[{'country': 'France', 'visits': 12, 'cities': ['Paris', 'Lille', 'Dijon']}, {'country': 'Germany', 'visits': 5, 'cities': ['Berlin', 'Hamburg', 'Stuttgart']}, {'country': 'Russia', 'visits': 2, 'cities': ['Moscow', 'Saint Petersburg']}]