cached
cached

Reputation: 589

Merge lists without ordering and not eliminating duplicates

(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

Answers (2)

b0bz
b0bz

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

nneonneo
nneonneo

Reputation: 179717

Use +:

a + b

This will create a new list which is the concatenation of the two input lists.

Upvotes: 4

Related Questions