Evans
Evans

Reputation: 21

How do merge 2 lists of different lengths in python

In Python 3, I'm looking got merge 2 lists of different lengths. I've went through a lot of the other threads here, but none seem to be addressing my issue.

I have:

List1 = ['A','B','C']
List2 = ['ab', 'ac', 'ad', 'ae']

Output = [['Aab', 'Aac', 'Aad', 'Aae'],['Bab','Bac','Bad','Bae'],['Cab','Cac','Cad','Cae']]

Thanks in advance!

UPDATE -- thanks ndclt for the solution for the above problem.

My real problem is:

List1 = ['A','B','C']
List2 = ['a','b','c','d','e','f','g','h','i']

Output desired = ['Aa', 'Ab', 'Ac', 'Bd', 'Be', 'Bf', 'Cg', 'Ch', 'Ci']

Upvotes: 0

Views: 366

Answers (5)

geekzeus
geekzeus

Reputation: 895

Ok so i am assuming your both lists length are divisible and gives integer as your data is provided ..

from operator import add

List1 = ['A','B','C']
List2 = ['a','b','c','d','e','f','g','h','i']

List = []

for i in List1:
    List.append(i*(len(List2)//len(List1)))

List1 = []

for i in List:
    for j in i:
    List1.append(j)

final_list = list(map(add, List1, List2))

#Output 
#['Aa', 'Ab', 'Ac', 'Bd', 'Be', 'Bf', 'Cg', 'Ch', 'Ci']

Upvotes: 0

Danilo
Danilo

Reputation: 1030

Result = [];
for i in List1:
    p = [];
    for j in List2:
        p.append( ''.join( [i,j] ) );
    Result.append(p);

Try this on for a size. The overall count that both loops would take is len(List1)*len(List2). But list declaration inside loop p = [] is initiated empty for every iteration of List1. The ''.join(list) joins elements of the list in a string with character. It is an part of string class.

You can find all functions for the module/class by typing dir(module) or dir(class) into interpreter.

Upvotes: 0

ndclt
ndclt

Reputation: 3108

Not really nice but, it works:

List1 = ['A','B','C']
List2 = ['ab', 'ac', 'ad', 'ae']

final = []
for one in List1:
    intermediate = []
    for two in List2:
        intermediate.append(one+two)
    final.append(intermediate)

print(final)
[['Aab', 'Aac', 'Aad', 'Aae'], ['Bab', 'Bac', 'Bad', 'Bae'], ['Cab', 'Cac', 'Cad', 'Cae']]

If nested is not mandatory:

from itertools import product
List1 = ['A','B','C']
List2 = ['ab', 'ac', 'ad', 'ae']

final = [one+two for one, two in product(List1, List2)]
print(final)
['Aab', 'Aac', 'Aad', 'Aae', 'Bab', 'Bac', 'Bad', 'Bae', 'Cab', 'Cac', 'Cad', 'Cae']

Upvotes: 0

SyntaxVoid
SyntaxVoid

Reputation: 2633

Here's a one-liner:

It creates your nested lists using for loop comprehensions.

List1 = ['A','B','C']
List2 = ['ab', 'ac', 'ad', 'ae']

out = [[an + bn for bn in List2] for an in List1]
print(out)
# >>> [['Aab', 'Aac', 'Aad', 'Aae'], ['Bab', 'Bac', 'Bad', 'Bae'], ['Cab', 'Cac', 'Cad', 'Cae']]

Upvotes: 4

hmn Falahi
hmn Falahi

Reputation: 777

Try this:

list1 = ['A','B','C']
list2 = ['ab', 'ac', 'ad', 'ae']
lst = []
finaly_lst = []
for i in list1:
    for j in list2:
        lst.append(i+j)
    finaly_lst.append(lst)
    lst = []
print(finaly_lst)

Output:

[['Aab', 'Aac', 'Aad', 'Aae'], ['Bab', 'Bac', 'Bad', 'Bae'], ['Cab', 'Cac', 'Cad', 'Cae']]

Upvotes: 0

Related Questions