Introduction You are probably already familiar with the Python concatenation operators (+ and +=). Concatenation operators can be used to combine multiple types of sequences (e.g., concatenate 2 lists). The same applies to and *= if you need to repeatedly use a particular sequence type. Both of these will produce identical results, as you already know. Many Python programmers think that my_list1+= my_list2 is a way to shorten my_list1 and my_list1. This argument is also valid for my_list1*= n, and my_list1= my_list *n. However it’s far from the truth. This article will explain the differences between + and +=, as well as vs*=. We will use a list to illustrate + and +=. If you attempt to combine two lists, mylist_1 & mylist_2, using the + operator it creates an object and assigns mylist_1 the object. You can see that mylist_1 & mylist_2 point to different locations. The concatenation also points to a different location. >>> mylist_1 = [1, 2, 3]
>>> mylist_2 = [4, 5]
>>> print(id(mylist_1), id(mylist_2))
1614327515784 1614319969800 >>> mylist_1 = mylist_1 + mylist_2
>>> print(mylist_1)
>>> print(id(mylist_1))
[1, 2, 3, 4, 5]
1614319969416 Now, look at the same example but using in-place concatenation operator +=. You can see that the += operator did not create an object. Mylist_1 points to the exact same location in memory before and after concatenation. Mylist_1 has been mutated. >>> mylist_1 = [1, 2, 3]
>>> mylist_2 = [4, 5]
>>> print(id(mylist_1), id(mylist_2))
1614325213896 1614316889416 >>> mylist_1 += mylist_2
>>> print(mylist_1)
>>> print(id(mylist_1))
[1, 2, 3, 4, 5]
1614325213896 However, for immutable objects such as int, floats, string, etc. it doesn’t matter. The memory location for num1 in each scenario is very different, as you can see from the following example. >>> num1 = 10
>>> num2 = 20
>>> id(num1), id(num2)
(140726841418416, 140726841418736) >>> num1 = num1 + num2
>>> id(num1)
140726841419056 >>> num1 += num2
>>> id(num1)
140726841419696 The difference between both the concatenation operators is that the + creates a new list and the += modifies an existing list in place. Similar to the concatenation operator we have just seen, and *= behave similarly with repetition operators. See the following example. Mylist1’s memory location is different after and before the repetition [1, 2, 3], operator. >>> mylist1 [1,2] >>> Id(mylist1) 2020051867784 Mylist1 = mylist13 >>> Print(mylist1) [1, 2, 1, 2, 1, 2] >>> ID(mylist1) 2020051883912 Let’s now look at another example with the *= operator. You can clearly see that mylist1’s memory location is identical before and after concatenation. Mylist1 has been mutated using the *= operator. >>> mylist1 [1,2] >>> Mylist1 *= 3 >>> print (mylist1) [1, 2, 1, 2, 1, 2] >>> ID(mylist1) 2020051837832 Because we cannot modify immutable objects, the in-place repetition operator doesn’t matter. There is a difference in the two repetition operators: the creates new lists and the *= changes existing ones. In-place concatenation (+=) and in-place repetition (*=) have many advantages. They are faster than any other concatenation/repeat operation. The in-place operation modifies the object, so Python does not have to create another object. This saves time. Comparison of concatenation speeds As you can see from the following example, += performs marginally better than the + operator. 2021 seconds, while the + operator took 2. 42 seconds whereas the += operator took 2. 29 seconds. + operator >>> code = “””
>>> mylist_1 = [1, 2, 3, 4, 5, 7, 8]
>>> mylist_2 = [9, 10]
>>> mylist_1 = mylist_1 + mylist_2
>>> “””
>>> import timeit
>>> timeit.timeit(stmt=code, number=10_000_000)
2. 428993300000002 += operator >>> code = “””
>>> mylist_1 = [1, 2, 3, 4, 5, 7, 8]
>>> mylist_2 = [9, 10]
>>> mylist_1 += mylist_2
>>> “””
>>> import timeit
>>> timeit.timeit(stmt=code, number=10_000_000)
2. 293214599999999 Comparison of repetition speed The operator took *operator >>> code = “””
>>> mylist_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> mylist_1 = mylist_1 2
>>> “”” >>> import timeit
>>> timeit.timeit(stmt=code, number=10_000_000)
4. 3188133 *= operator >>> code = “””
>>> mylist_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> mylist_1 *= 2
>>> “”” >>> import timeit
>>> timeit.timeit(stmt=code, number=10_000_000)
4. 291261399999999 This article explains the difference between +, + and operators. The + and operators create new lists, while the + and *= operator modifies existing ones. Originally published at pythonsimplified on June 22, 2021. Additional Reading







