0

Having 2 lists, can be different or same lengths.

a = [('1','1','2'), ('2','2','2','2'), ('3','3')]
b = [('7','8','8','9'), ('0','0','4','5')]

How can I add / concat the lists such that I'll get the following output.

Also, not sure if I'm using the right terminology here. Appreciate any corrections.

c = [
('1','1','2','7','8','8','9'),('1','1','2','0','0','4','5'),
('2','2','2','2','7','8','8','9'),('2','2','2','2','0','0','4','5'),
('3','3','7','8','8','9'),('3','3','0','0','4','5')
]

Essentially, c = [ (a[0] + b[0]), (a[0] + b[1]), (a[1] + b[0]), (a[1] + b[1]).....

Thus far, I've just been using for loops. I've looked at itertools.product, but the output isn't right. Additionally, if I increase it to 3 lists, then the combination becomes larger.

d123
  • 1,497
  • 2
  • 12
  • 22

2 Answers2

2

You were on the right track, thinking to use product.

from itertools import product

a = [('1','1','2','3','4','5','6'), ('2','2','2','2','2','2','2'), ('3','3','2','1','1','1','1')]
b = [('7','8','8','9'), ('0','0','4','5')]

paired_up = product(a, b)
c = [sum(tuples, start=()) for tuples in x]  # The default start is 0, which leads to TypeErrors.

print(c)
# [('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9'),
#  ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5'),
#  ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9'),
#  ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5'),
#  ('3', '3', '2', '1', '1', '1', '1', '7', '8', '8', '9'),
#  ('3', '3', '2', '1', '1', '1', '1', '0', '0', '4', '5')]

More than two lists? Pass them all to product.

d = [('0',), ('1',)]  # Let's add another list to the mix.
paired_up = product(a, b, d)  # It gets passed to `product` with the rest.
c = [sum(tuples, start=()) for tuples in paired_up]
print(c)
# [('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9', '0'),
#  ('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9', '1'),
#  ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5', '0'),
#  ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5', '1'),
#  ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9', '0'),
#  ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9', '1'),
#  ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5', '0'),
#  ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5', '1'),
#  ('3', '3', '2', '1', '1', '1', '1', '7', '8', '8', '9', '0'),
#  ('3', '3', '2', '1', '1', '1', '1', '7', '8', '8', '9', '1'),
#  ('3', '3', '2', '1', '1', '1', '1', '0', '0', '4', '5', '0'),
#  ('3', '3', '2', '1', '1', '1', '1', '0', '0', '4', '5', '1')]
Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
  • If there were 3 lists, would I just do triplets = product(paired_up, c) d = list(map(sum_tuples, triplets)) – d123 Mar 05 '21 at 05:43
  • It would be even better to do `product(a, b, c)`. – Arya McCarthy Mar 05 '21 at 05:44
  • oh, ok. I didn't know I can do that. I'll have to go read up. Thank you – d123 Mar 05 '21 at 05:45
  • Added an example of this. – Arya McCarthy Mar 05 '21 at 05:47
  • Thanks! Can I also assume this is a generator? If I have very large lists, I'm a little worried about memory issues. – d123 Mar 05 '21 at 05:59
  • It's straightforward to [convert the list comprehension to a generator expression](https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehensions)—turn the brackets into parentheses. In that case, yes. – Arya McCarthy Mar 05 '21 at 06:02
  • If I have 3 lists, 15k x 40k x 12k, might you happen to know a way I can efficiently and quickly filter them? Currently I've used your above solution but converted `c` into a generator and am running filter() against it. – d123 Mar 05 '21 at 07:23
1

toRex already provided you the answer as I was typing this.

a = [('1','1','2','3','4','5','6'),
     ('2','2','2','2','2','2','2'),
     ('3','3','2','1','1','1','1')]
b = [('7','8','8','9'),
     ('0','0','4','5')]

print ([(i+j) for i in a for j in b])

The output will be:

[('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9'), 
 ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5'), 
 ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9'), 
 ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5'), 
 ('3', '3', '2', '1', '1', '1', '1', '7', '8', '8', '9'), 
 ('3', '3', '2', '1', '1', '1', '1', '0', '0', '4', '5')]

Input:

a = [('1','1','2','3','4','5','6'),
     ('2','2','2','2','2','2','2'),
     ('3','3','2','1','1','1','1','5','5','5'),
     ('1','2','3')]

b = [('7','8','8','9'),
     ('0','0','4','5'),
     ('1','2')]

Output:

[('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9'), 
 ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5'), 
 ('1', '1', '2', '3', '4', '5', '6', '1', '2'), 
 ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9'), 
 ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5'), 
 ('2', '2', '2', '2', '2', '2', '2', '1', '2'), 
 ('3', '3', '2', '1', '1', '1', '1', '5', '5', '5', '7', '8', '8', '9'),
 ('3', '3', '2', '1', '1', '1', '1', '5', '5', '5', '0', '0', '4', '5'),
 ('3', '3', '2', '1', '1', '1', '1', '5', '5', '5', '1', '2'), 
 ('1', '2', '3', '7', '8', '8', '9'), 
 ('1', '2', '3', '0', '0', '4', '5'), 
 ('1', '2', '3', '1', '2')]
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33