Python Simplified

# Know the difference between a=a+b and a+=b

## Introduction

Do you know the difference between a=a+b and a+=b? Not just that, a=a*n is also not exactly same as a*=n! I am sure that you are well aware of these operators, and most likely using it interchangeably. There is no harm though, as both gives the same result. So if both gives the same result, then what is the difference? Well, read on…

## + and +=

Let’s look at an example below using a list. When you try to concatenate two lists mylist_1 and mylist_2 using the + operator, it creates a new object and assigns mylist_1 to the new object.

As you can see mylist_1 and mylist_2 are pointing to two different memory locations. And the result of the concatenation is also pointing to a different memory 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, 4, 5]
1614319969416
```
```

Now, look at the same example but using in-place concatenation operator +=. As you can see, the += operator didn’t create a new object. The mylist_1 is pointing to the same memory location before and after the concatenation. We can say that mylist_1 is 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. As seen from the below example, the memory location of num1 is different before and after the concatenation in both scenarios.

```				```
>>> 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.

## * and *=

The repetition operators also behave similarly to the concatenation operators we just went through. Refer to the example below. The memory location of mylist1 is different before and after the repetition (*) operator.

```				```
>>> mylist1 = [1,c2]
>>> id(mylist1)
2020051867784

>>> mylist1 = mylist1 * 3
>>> print(mylist1)
[1, 2, 1, 2, 1, 2]

>>> id(mylist1)
2020051883912
```
```

Now, let’s look at the same example using the *= operator. As you can see, the memory location of mylist1 is the same before and after the concatenation operation. So, mylist1 is mutated with the *= operator.

```				```
>>> mylist1 = [1,2]
>>> id(mylist1)
2020051837832

>>> mylist1 *= 3
>>> print(mylist1)
[1, 2, 1, 2, 1, 2]

>>> id(mylist1)
2020051837832
```
```

The difference between both the repetition operators is that the * creates a new list and the *= modifies an existing list in place.

The main benefit of using in-place concatenation (+=) or in-place repetition (*=) is that it is faster than normal concatenation or repetition operation. Because the in-place operation mutates the object and Python doesn’t have to create a new object which saves some time.

### Concatenation operator speed comparison

As seen from the below example, += is marginally faster than the + operator.

+ 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
```
```

### Repetition operator speed comparison

* 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)
2.8596363000000338
```
```

*= 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)
2.5640303999999787
```
```

#### Chetan Ambi

A Software Engineer & Team Lead with over 10+ years of IT experience, a Technical Blogger with a passion for cutting edge technology. Currently working in the field of Python, Machine Learning & Data Science. Chetan Ambi holds a Bachelor of Engineering Degree in Computer Science.
Scroll to Top