Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 176
    What is the difference between extend and simply using the addition operator - in the above example, x = x + [4, 5]? Commented May 29, 2017 at 22:10
  • 423
    Actually there's a big difference - x + [4, 5] gives you a new list assigned to x - x.extend() mutates the original list. I elaborate in my answer here below. Commented Jul 17, 2017 at 17:14
  • 12
    @AaronHall @Rohan but it is same as x += [4,5]. Commented Dec 13, 2018 at 14:54
  • 3
    The keyword when using append is Object. If you try to use extend and you pass in a dictionary, it will append the key, and not the whole hash to the end of the array. Commented Mar 15, 2019 at 14:53
  • 7
    @Rohan, the time complexity of x = x + [4, 5] would be O(len(x) + len([4,5])) where as extend has the time complexity of O(len([4, 5])) Commented Jul 22, 2020 at 16:20