0

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']
Evans
  • 21
  • 2
  • does the result have to be a *nested* list? – jeschwar Jul 25 '19 at 21:52
  • 3
    what happens if `List2 = ['a','b','c','d','e','f','g','h','i','j']`? BUT, there are already 4 answers answering your original question. It is not nice for the answerers that took the time to answer to change the question suddenly. I suggest you leave the question as it is and accept the best answer and add a new question with your real problem – Tomerikoo Jul 25 '19 at 21:57
  • Does the desired output HAVE to be sorted in the same order you provided? – SyntaxVoid Jul 25 '19 at 22:03
  • What's the rule for the combination of letters - always 3, or based on the length of `List1` or something else? – DaveStSomeWhere Jul 25 '19 at 22:03
  • Anyway, this is for your update: `res = [list1[i//(len(list2)//len(list1))]+list2[i] for i in range(len(list2))]` – Tomerikoo Jul 25 '19 at 22:08
  • Thanks @Tomerikoo - Yah I apologize. Sorry about that. – Evans Jul 25 '19 at 22:45
  • See https://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists for the general case of wanting to process the cartesian product of a bunch of lists. – Andrew McClement Aug 02 '21 at 17:58

5 Answers5

4

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']]
SyntaxVoid
  • 2,501
  • 2
  • 15
  • 23
0

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']
ndclt
  • 2,590
  • 2
  • 12
  • 26
0

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']]
hmn Falahi
  • 730
  • 5
  • 22
0
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.

Danilo
  • 1,017
  • 13
  • 32
  • You are aware that lines in Python do not end in `;` and that there are already 2 answers offering that same solution? – Tomerikoo Jul 25 '19 at 22:12
  • I am aware and I am also aware that no-one used string based function but overloaded + operator. And that no-one gave any information about looking and researching modules from inside interpreter. Also ending of each lines in python with `;` makes code readable and more interpreter friendly. In general only lines in continuation must contain that symbol, but is advised by pythoniacs to assure realibility and realibility that in large projects ( such as guis) this rule is followed. – Danilo Jul 25 '19 at 22:20
0

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']
geekzeus
  • 785
  • 5
  • 14