Sook Yee Lim - 1 year ago 201
Python Question

# How to combine two lists into a third list?

I got three list of lists:

``````A=[[9, 'a'], [0, 'c'], [2, 'g'], [7, 'w'], [0, 'Q']]
B=[[0, 'a'], [0, 'c'], [0, 'w'], [0, 'Q'], [3, 'front'], [5, 'Z']]
C=[[9, 'a'], [0, 'c'], [7, 'w'], [0, 'Q']]
``````

and I want to combine A and B into C like this:

``````C=[[9, 'a'], [0, 'c'],[7, 'w'], [0, 'Q'],[2,'g'], [3,'front'],[5,'Z']]
``````

Basically list with g is not in C so I append into C.Same for list B.

EDIT:I do not need a specific index just append into C.

``````from itertools import zip_longest
for x,y in zip_longest(enumerate(A),enumerate(B)):
for i in x,y:
if i is not None and i[1][1] not in map(lambda i: i[1], C):
if(i[0] < len(C)):
C.insert(i[0], i[1])
else:
C.append(i[1])
print(C)
``````

In order to have the location data as you outlined, this approach seems to work. It's output is: [[3, 'front'], [5, 'Z'], [7, 'w'], [0, 'Q'], [0, 'c'], [0, 'w'], [9, 'a'], [2, 'g'], [0, 'a']]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download