Reputation: 589
(Python) I have 2 lists and want to merge them as follows.
a = [2,5,1]
b = [4,2,2]
Combine lists and the expected output should be: [2,5,1,4,2,2]
Upvotes: 0
Views: 88
Reputation: 2200
There is also the extend function, this simply extends a
with b
:
a = [2,5,1]
b = [4,2,2]
a.extend(b)
To make a new list, eg: c
one can do something like the following, even though nneonneo answer is simpler..:
def extendList(a, b):
a.extend(b)
return a
a = [2,5,1]
b = [4,2,2]
c = extendList(a, b)
Upvotes: 0
Reputation: 179717
Use +
:
a + b
This will create a new list which is the concatenation of the two input lists.
Upvotes: 4