# Printing combinations of two lists in Python

Stack Overflow Asked on January 3, 2022

What I’m trying to print is the following, using 2 variables in each string for simplicity. I think something’s wrong using the loops, i’m kinda new to Python. Thanks in advance!

com0 <-(a) with (1,)
com1 <-(a) with (2,)
com2 <-(a) with (1, 2)
com3 <-(b) with (1,)
com4 <-(b) with (2,)
com5 <-(b) with (1, 2)
com6 <-(a,b) with (1,)
com7 <-(a,b) with (2,)
com8 <-(a,b) with (1, 2)


This is what I’ve tried:

import itertools

i = 0 #v1
j = 0 #v2

v1 = [1, 2]
v2 = ["a","b"]

while j < 2**len(v2):

for K in range(0, len(v2)+1):

while i < 2**len(v1):

for L in range(0, len(v1)+1):

for subset2 in itertools.combinations(v1, L):

for subset1 in itertools.combinations(v2, K):

print("com{0} <-{1} with {2}".format(i,subset1,subset2))

i+=1
j+=1


You dont need these many loops for this. Just use combinations and product from itertoools

>>> from itertools import combinations, product
>>>
>>> v1 = [1, 2]
>>> v2 = ["a","b"]
>>>
>>> all_v1 = [e for i in range(len(v1)) for e in combinations(v1,i+1)]
>>> all_v2 = [e for i in range(len(v1)) for e in combinations(v2,i+1)]
>>>
>>> for i, (x,y) in enumerate(product(all_v2, all_v1)):
...     print (f'com{i} <-{x} with {y}')
...
com0 <-('a',) with (1,)
com1 <-('a',) with (2,)
com2 <-('a',) with (1, 2)
com3 <-('b',) with (1,)
com4 <-('b',) with (2,)
com5 <-('b',) with (1, 2)
com6 <-('a', 'b') with (1,)
com7 <-('a', 'b') with (2,)
com8 <-('a', 'b') with (1, 2)


Answered by Prem Anand on January 3, 2022

The product() from itertools results in the Cartesian product of the two lists. By using this function you avoid all the loops:

import itertools

v1 = [1, 2]
v2 = ["a","b"]
combinations = list(itertools.product(v1, v2))
>> [(1, "a"), (1, "b"), (2, "a"), (2, "b")]


Answered by Bram Dekker on January 3, 2022

## Related Questions

### Anomalous scanf behaviour in C

3  Asked on November 17, 2021 by parth-sarthi-sharma

### Stop message box on a given situation Excel VBA

1  Asked on November 17, 2021

### Javascript/Typescript – Can’t map everything to a table

1  Asked on November 17, 2021 by shineonbro

### Javascript re-order array of object by value

1  Asked on November 17, 2021

### Is there any way to cast timedelta64 to int64 in numba?

2  Asked on November 17, 2021 by anton-philippoff

### How to include identical if block in multiple functions?

5  Asked on November 16, 2021

### How do I make certain command arguments?

1  Asked on November 16, 2021 by yeti

### How can I use Jenkins Sidebar Link Plugin in pipeline step?

3  Asked on November 16, 2021 by 13kz

### Why does Vec expect &T as the argument to binary_search?

1  Asked on November 16, 2021 by grexis

### Programatically (C#) update QC/ALM Test Plan > Test Script tab content

1  Asked on November 16, 2021 by user4619697

1  Asked on November 16, 2021 by andy-delworth

### Is it possible to hide data in a bitmap using LockBits?

1  Asked on November 16, 2021 by jeremy-james

### Sorting by timestamp doesn’t provide accurate recent record

0  Asked on November 16, 2021 by user13599244

### 2nd order ODEs in Python – defining the differential

2  Asked on November 16, 2021 by a0142204

### Nuxt static site with headless wordpress – including images in generated build

0  Asked on November 16, 2021 by zach-g

### Repeated filtering on Spark Dataframe?

2  Asked on November 16, 2021 by jeff-gong

1  Asked on November 16, 2021 by eli-chen

### Python List: Replace Double Quotes with Single Quotes

3  Asked on November 16, 2021 by dataplumber

### Issue : Cant resolve symbol ‘firebasemessaging’

0  Asked on November 16, 2021 by sarvesh-dalvi

### Map a dynamic json key to a class field using JPA

1  Asked on November 16, 2021