2

Beginner question: If I have 2 lists containing only strings, how do I concatenate them in a third list containing all the elements + each element of one list + each element in the other list? I thought about for loops, but isn't there a more simple way to do it? Example:

listone = ['cat', 'dog' ]
listtwo = ['bin', 'sun'] 

resulting list = ['cat', 'dog', 'bin', 'sun', 'catbin', 'catsun', 'dogbin', 'dogsun', 'catbinsun', 'catsunbin', 'catcat', 'catdog', 'dogbin', 'dogcat']

EDIT: Thank you all for replies, but i didn't explain what I want to do well enough. The number of string in each list should be indefinite, and every word has to be concatenated with every other word, but not only in the form "x + y". I want also to concatenate it to the other words. Like u = x+y+z

6 Answers 6

3

You can use Cartesian product and join. Try this:

import itertools
listone = ['cat', 'dog' ]
listtwo = ['bin', 'sun'] 
listone + listtwo + list(''.join(e) for e in itertools.product(listone, listtwo))

Result:

['cat', 'dog', 'bin', 'sun', 'catbin', 'catsun', 'dogbin', 'dogsun']
Sign up to request clarification or add additional context in comments.

Comments

2

This should do the job

In [29]: import itertools
In [30]: listone = ['cat', 'dog' ]
    ...: listtwo = ['bin', 'sun']

In [31]: output = listone + listtwo + [ "".join(items)  for items in list(itertools.product(listone, listtwo))]

In [32]: output
Out[32]: ['cat', 'dog', 'bin', 'sun', 'catbin', 'catsun', 'dogbin', 'dogsun']

You also could do it without itertools

In [33]: output = listone + listtwo + [ item_one+item_two for item_one in listone for item_two in listtwo]

In [34]: output
Out[34]: ['cat', 'dog', 'bin', 'sun', 'catbin', 'catsun', 'dogbin', 'dogsun']

2 Comments

How would this work with more than two elements? Brief example: i want ['a','b','c'] and ['c', 'd', 'e'] to also return [.....'ab', 'ac' 'abc', 'abcde', 'cd', ce,.. et cetera]
@Kodeeo post this as another SO and I will answer it. It requires combinations. Thanks.
1

You can get this result with comparatively simple code if you use list comprehensions:

listone = ['cat', 'dog' ]
listtwo = ['bin', 'sun'] 
result_list = listone + listtwo + [x + y for x in listone for y in listtwo]

Comments

0

Without importing itertools, you can also use string concatenation when accessing the elements of listone and listtwo like so:

result = listone + listtwo + [listone[0] + listtwo[0], listone[0] + listtwo[1], listone[1] + listtwo[0], listone[1] + listtwo[1]]

2 Comments

I understand you don't want to import libraries. But what is this? Can't you write for loop?
@mamun when asking the question he said he had already thought about using a for loop, so with only the two lists that he provided this is an alternative without a loop. You could also easily turn this into a loop for more elements.
0

Or if you want to do it by hand I would do it this way :

l = ["a","b"]
l2 = ["c","d"]
l3 = l + l2
print(l3)
for x, y in zip(l,l2):
    l3.append(x+y)
print(l3)

I don't know if it's optimal tho.

Comments

0

If you're only looking for combinations (i.e. x+y,x+y+z but not y+x or z+y+x), you could use the combinations functions from itertools to form a power set:

from itertools import combinations

words  = listone+listtwo
result = ["".join(c) for r in range(1,len(words)+1) for c in combinations(words, r)]

print(result)

# ['cat', 'dog', 'bin', 'sun', 'catdog', 'catbin', 'catsun', 'dogbin',
#  'dogsun', 'binsun', 'catdogbin', 'catdogsun', 'catbinsun', 'dogbinsun',
#  'catdogbinsun']

note: combinations power sets grow exponentially with the number of source items: 2^n - 1

If you want to obtain permutations instead, replace combinations by permutationsfrom itertools. (this will produce an order of magnitude more elements than combinations)

Comments

Your Answer

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