Consider I have two lists in python
a = [1, 2, 3]
and b = [4, 5]
. I want to concatenate these two lists and it doesn't matter if the existing object changes.
As far as I know, we can do this in two ways as follow
a.extend(b)
, here a
will have the concatenated result i.e [1, 2, 3, 4, 5]
c = a + b
, c
will have the same result.
I know that the first one mutating the same object while the second creates a new object. But which one is more costlier in terms of performance? Which method is advisable?