tags:

views:

202

answers:

4

Just as it sounds:

listone = [1,2,3]
listtwo = [4,5,6]

#outcome we expect: mergedlist == [1, 2, 3, 4, 5, 6]
+7  A: 

Python makes this ridiculously easy.

mergedlist = listone + listtwo
Daniel G
Wow that is easy, thanks :p
Joshua
+2  A: 

This is quite simple, I think it was even shown in the tutorial:

>>> listone = [1,2,3]
>>> listtwo = [4,5,6]
>>>
>>> listone + listtwo
[1, 2, 3, 4, 5, 6]
Tuure Laurinolli
+1  A: 

It's also possible to create a generator that simply iterates over the items in both lists. This allows you to chain lists (or any iterable) together for processing without copying the items to a new list:

import itertools
for item in itertools.chain(listone, listtwo):
   # do something with each list item
Robert Rossney
A: 

You can use sets to obtain merged list of unique values

mergedlist = list(set(listone + listtwo))

Radagast
this will lose ordering information.
aaronasterling