Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

The difference between a=a+b and a+=b in Python
Programming

The difference between a=a+b and a+=b in Python

Last Updated on November 3, 2021 by Editorial Team

Author(s): Chetan Ambi

Introduction

I am sure you are already aware of the concatenation operators + and += in Python. As the name itself says, concatenation operators are used to concatenating multiple sequence types (For example, concatenate two lists). Similarly, * and *= are used when you want to repeat a sequence type n times. You also know that both these produce the same results.

But many Python developers think my_list1 += my_list2 as a shorthand notation to my_list1 = my_list1 + my_list2. The same argument holds true for my_list1 *= n and my_list1 = my_list * n. But its far from true.

In this short article, you are going to learn the difference between + vs += and * vs *=.


+ 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]
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,2]
>>> 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 in-place repetition operator also doesn’t make difference for an immutable object because we can’t modify an immutable object.

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

Advantages

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 speed comparison

As seen from the below example, += is marginally faster than the + operator. 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

Repetition speed comparison

As seen from the below example, *= is marginally faster than the *operator. 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

Conclusion

In this article, you have understood an important difference between + and += and * and *= operators. Both * and + operator create a new list whereas t += and *= modifies an existing list in place.

Originally published at pythonsimplified on June 22, 2021.

Further Reading

Published via Towards AI

Feedback ↓