What is really the difference between += operation and append function in python lists? inside functions definations
Question:
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)
Answers:
While using the operator ‘+’, you should create the list
travel_log = []
before using it.
def add_new_country(Country,Visits,Cities):
travel_log = []
travel_log+={"country":Country,"visits":Visits,"cities":Cities}
this is because you used the travel_log
variable in a function and not mentioning that you mean the global variable
this will fix the issue:
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):
global travel_log
travel_log += [{"country":Country,"visits":Visits,"cities":Cities}]
add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])
print(travel_log)
the difference is when you call a method from travel_log
, it grabs the global variable and calls the method, but when you use assignment operators python thinks you meant to create a local variable and therefore it says referenced before assignment
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)
While using the operator ‘+’, you should create the list
travel_log = []
before using it.
def add_new_country(Country,Visits,Cities):
travel_log = []
travel_log+={"country":Country,"visits":Visits,"cities":Cities}
this is because you used the travel_log
variable in a function and not mentioning that you mean the global variable
this will fix the issue:
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):
global travel_log
travel_log += [{"country":Country,"visits":Visits,"cities":Cities}]
add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])
print(travel_log)
the difference is when you call a method from travel_log
, it grabs the global variable and calls the method, but when you use assignment operators python thinks you meant to create a local variable and therefore it says referenced before assignment