-1

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)
  • https://stackoverflow.com/questions/2347265/why-does-behave-unexpectedly-on-lists – Devang Sanghani Feb 23 '22 at 08:00
  • 3
    It's strange that you compare `+=` to the `append` method in the first place, since the `+=` operator of a list works much closer to the `extend` method, which "extends" a list with items extracted from a given iterable. – blhsing Feb 23 '22 at 08:03
  • 1
    In order to get it to work with the `+=` operator, add `global travel_log` to the function. Then use: `travel_log+=[{"country":Country,"visits":Visits,"cities":Cities}]`. When using `+=` the type has to be the same in order to keep the same structure. – Cow Feb 23 '22 at 08:09
  • 2
    While I am sure there is an appropriate duplicate to this, none of the ones used/proposed so far seems appropriate. The important part is that augmented assignments, like any other assignment, imply that the target is local. A duplicate should at least cover that, it does not necessarily have to cover lists. – MisterMiyagi Feb 23 '22 at 10:53
  • @MisterMiyagi so basically what I could not find was what the last para in the first answer tells me, if you find a duplicate of that I think that would be the perfect merge! – Priyanshu Upadhyay Feb 23 '22 at 12:35

2 Answers2

2

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

No.BoD
  • 151
  • 9
-2

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}
  • 1
    Actually doing this will result in completely clearing out the initial list so the whole point of creating a function to do so will be irrelevant. – Priyanshu Upadhyay Feb 23 '22 at 10:41