So till now the only real difference that I knew about the difference in the append and += operation was that of speed but recently I stumbled upon a case in which += would throw an error while append would not can someone help out with what has been happening?
This code would not run and throw an error namely UnboundLocalError: local variable 'travel_log' referenced before assignment
travel_log = [
{
"country": "France",
"visits": 12,
"cities": ["Paris", "Lille", "Dijon"]
},
{
"country": "Germany",
"visits": 5,
"cities": ["Berlin", "Hamburg", "Stuttgart"]
},
]
def add_new_country(Country,Visits,Cities):
travel_log+={"country":Country,"visits":Visits,"cities":Cities}
add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])
print(travel_log)
while this code would run
travel_log = [
{
"country": "France",
"visits": 12,
"cities": ["Paris", "Lille", "Dijon"]
},
{
"country": "Germany",
"visits": 5,
"cities": ["Berlin", "Hamburg", "Stuttgart"]
},
]
def add_new_country(Country,Visits,Cities):
travel_log.append({"country":Country,"visits":Visits,"cities":Cities})
add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])
print(travel_log)