1

I have two lists of lists:

List1 = [['Jack','27','cat','house','fireman'], ['Tom','43','cat','Flat','Doctor'], ['Ben','30','Dog','house','Postman']]

List2 = [['Orange','5','Tom'], ['Blue','11','Ben'], ['Red','21','Jack']]

I want to join the lists together where the name matches:

Result_list = [['Jack','27','cat','house','fireman','Red','21','Jack'], ['Tom','43','cat','Flat','Doctor','Orange','5','Tom'], ['Ben','30','Dog','house','Postman','Blue','11','Ben']]

The names will always be at position [0] within the lists in list1 and [2] within the lists in list2

I'm am pretty new to python and cannot work out how to do this. Is anyone able to offer a solution?

Thanks

4 Answers 4

1

This is one approach by converting one of the list to a dict.

List1 = [['Jack','27','cat','house','fireman'], ['Tom','43','cat','Flat','Doctor'], ['Ben','30','Dog','house','Postman']]
List2 = [['Orange','5','Tom'], ['Blue','11','Ben'], ['Red','21','Jack']]

List2_d = {i[-1]: i for i in List2}
for j in List1:
    if j[0] in List2_d:
        j.extend(List2_d[j[0]])

print(List1) 

Output:

[['Jack', '27', 'cat', 'house', 'fireman', 'Red', '21', 'Jack'],
 ['Tom', '43', 'cat', 'Flat', 'Doctor', 'Orange', '5', 'Tom'],
 ['Ben', '30', 'Dog', 'house', 'Postman', 'Blue', '11', 'Ben']]
Sign up to request clarification or add additional context in comments.

Comments

1
list1 = [['Jack','27','cat','house','fireman'], 
['Tom','43','cat','Flat','Doctor'], 
['Ben','30','Dog','house','Postman']]

list2 = [['Orange','5','Tom'], ['Blue','11','Ben'], ['Red','21','Jack']]

for i in list1:
    for j in list2:
        if i[0] == j[2]:
            i.extend(j)
print(list1)

output:

[['Jack', '27', 'cat', 'house', 'fireman', 'Red', '21', 'Jack'],
 ['Tom', '43', 'cat', 'Flat', 'Doctor', 'Orange', '5', 'Tom'],
 ['Ben', '30', 'Dog', 'house', 'Postman', 'Blue', '11', 'Ben']]

Comments

1

You can do this using a dictionary as in index to List2 and build the merged list with a comprehension on List1 that concatenates the matching sublist in the dictionary:

List1 = [['Jack','27','cat','house','fireman'], ['Tom','43','cat','Flat','Doctor'], ['Ben','30','Dog','house','Postman']]
List2 = [['Orange','5','Tom'], ['Blue','11','Ben'], ['Red','21','Jack']]

match  = { sl[2]:sl for sl in List2 }
merged = [ sl+match[sl[0]] for sl in List1 ]

output:

print(merged)

[['Jack', '27', 'cat', 'house', 'fireman', 'Red', '21', 'Jack'],
 ['Tom', '43', 'cat', 'Flat', 'Doctor', 'Orange', '5', 'Tom'],
 ['Ben', '30', 'Dog', 'house', 'Postman', 'Blue', '11', 'Ben']]

Comments

0

You could use a defaultdict to group by the names:

from collections import defaultdict

List1 = [['Jack','27','cat','house','fireman'], ['Tom','43','cat','Flat','Doctor'], ['Ben','30','Dog','house','Postman']]

List2 = [['Orange','5','Tom'], ['Blue','11','Ben'], ['Red','21','Jack']]

d = defaultdict(list)
for lst in List1:
    d[lst[0]].extend(lst)

for lst in List2:
    d[lst[-1]].extend(lst)

print([v for v in d.values()])

Since both lists are of the same length, we can also get away with zip here:

d = defaultdict(list)

for x, y in zip(List1, List2):
    d[x[0]].extend(x)
    d[y[-1]].extend(y)

print([v for v in d.values()])

Output:

[['Jack', '27', 'cat', 'house', 'fireman', 'Red', '21', 'Jack'], ['Tom', '43', 'cat', 'Flat', 'Doctor', 'Orange', '5', 'Tom'], ['Ben', '30', 'Dog', 'house', 'Postman', 'Blue', '11', 'Ben']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.