Skip to main content

This is my first question inon Stack Overflow.

I want to add a new dictionary at the end of an existing list with multiple dictionaries (see example below):

travel_log = [ { "country": "France", "visits": 12, "cities": ["Paris", "Lille", "Dijon"] }, { "country": "Germany", "visits": 5, "cities": ["Berlin", "Hamburg", "Stuttgart"] }, ]

new_country = { "country": country, "visits": int(visits), "cities": list_of_cities, }

travel_log = [
  {
    "country": "France",
    "visits": 12,
    "cities": ["Paris", "Lille", "Dijon"]
  },
  {
    "country": "Germany",
    "visits": 5,
    "cities": ["Berlin", "Hamburg", "Stuttgart"]
  },
]

new_country = {
    "country": country,
    "visits": int(visits),
    "cities": list_of_cities,
  }

new_countrynew_country needs to be addeadded to the list of travel_logtravel_log, but for a certain reason if I write:

travel_log += new_countrytravel_log += new_country

it willIt does not work, while

travel_log.append(new_country)travel_log.append(new_country)

will give the correct result.

I thought until now that the +=+= operator could be used in lists quite easily, but I am now a bit confused. Thank you in advance for your answers.

This is my first question in Stack Overflow.

I want to add a new dictionary at the end of an existing list with multiple dictionaries (see example below):

travel_log = [ { "country": "France", "visits": 12, "cities": ["Paris", "Lille", "Dijon"] }, { "country": "Germany", "visits": 5, "cities": ["Berlin", "Hamburg", "Stuttgart"] }, ]

new_country = { "country": country, "visits": int(visits), "cities": list_of_cities, }

new_country needs to be adde to the list of travel_log, but for a certain reason if I write:

travel_log += new_country

it will not work, while

travel_log.append(new_country)

will give the correct result.

I thought until now that the += operator could be used in lists quite easily, but I am now a bit confused. Thank you in advance for your answers.

This is my first question on Stack Overflow.

I want to add a new dictionary at the end of an existing list with multiple dictionaries (see example below):

travel_log = [
  {
    "country": "France",
    "visits": 12,
    "cities": ["Paris", "Lille", "Dijon"]
  },
  {
    "country": "Germany",
    "visits": 5,
    "cities": ["Berlin", "Hamburg", "Stuttgart"]
  },
]

new_country = {
    "country": country,
    "visits": int(visits),
    "cities": list_of_cities,
  }

new_country needs to be added to the list of travel_log, but for a certain reason if I write:

travel_log += new_country

It does not work, while

travel_log.append(new_country)

will give the correct result.

I thought until now that the += operator could be used in lists quite easily, but I am now a bit confused. Thank you in advance for your answers.

Source Link

When to use append() and when the += operator in Python?

This is my first question in Stack Overflow.

I want to add a new dictionary at the end of an existing list with multiple dictionaries (see example below):

travel_log = [ { "country": "France", "visits": 12, "cities": ["Paris", "Lille", "Dijon"] }, { "country": "Germany", "visits": 5, "cities": ["Berlin", "Hamburg", "Stuttgart"] }, ]

new_country = { "country": country, "visits": int(visits), "cities": list_of_cities, }

new_country needs to be adde to the list of travel_log, but for a certain reason if I write:

travel_log += new_country

it will not work, while

travel_log.append(new_country)

will give the correct result.

I thought until now that the += operator could be used in lists quite easily, but I am now a bit confused. Thank you in advance for your answers.