+ 4

# itertools: product

I made it pass the question in python lesson on itertools but while learning but i am curious to how it reached the conclusion of “6”, I don’t want to continue the lesson until is grasp this. from itertools import product a={1, 2} print(len(list(product(range(3), a))) #what does list mean in this line? It is said the ‘product’ is used to make possible combinations but how did reach 6? Can anyone shine some light on this question? Thanks

23rd Jan 2019, 12:13 PM
Dingo + 8
Click run and see combinations, len just counting them. https://code.sololearn.com/cANRNjct1Nz7/?ref=app
23rd Jan 2019, 12:42 PM
Hubert Dudek + 5
You just create list [a, b, c, ...] with all combinations of {1,2} and [0,1,2]. [0,1,2] is result of range(3).
23rd Jan 2019, 12:35 PM
Hubert Dudek + 5
Try to remember how we created quadratic equations by manually squaring x+y to get x^2+xy+yx+y^2. its simply x(x+y)+y(x+y) in our case we have set a as {1,2} and another iterable created by range(3) which is [0,1,2] the product ideally becomes 1×(0,1,2) + 2(0,1,2). so you get (1,0),(1,1),(1,2) and (2,0),(2,1),(2,2). and those are six tuples
1st Aug 2019, 10:04 AM
Sost + 5
6
11th Jan 2020, 7:26 AM
Babeetha Muruganantham + 2
Alrighty, so the code is building a list of the different combinations of "a" (1,2) the range is 3 which will give you the following list (0,1)(1,1)(2,1)(0,2)(1,2)(2,2). If you run print(list(product(range(3), a))) #that's without "len" it will print out that list. So if you count the number of listed items the answer is 6.
22nd Jul 2020, 7:30 AM
Tyler Sandiford + 1
Hubert Dudek ,Thank you! you beautiful person I didn't take the len into account when thinking this... thank you again
23rd Jan 2019, 12:50 PM
Dingo + 1
Thanks to Babeetha
30th Jun 2020, 7:24 AM
Faith Mundi 0
[0,2]wha?? I get what you were trying to say about the combinations but... the output is 6... how excatly did it reach it?
23rd Jan 2019, 12:36 PM
Dingo 0
What is the output of this code? from itertools import product a={1, 2} print(len(list(product(range(3), a)))) output:6
28th Apr 2020, 7:41 AM
makhan Kumbhkar 0
The ans is 6
30th Jun 2020, 7:24 AM
Faith Mundi 0
What is the output of this code? from itertools import product a={1, 2} print(len(list(product(range(3), a)))) [(0, 1), (0, 2), (1, 1), (1, 2), (2, 1) , (2, 2)] #there are 6 possible value # range(3) represent (0,1,2)
12th Oct 2021, 6:42 PM
Pratap Vasava 0
from itertools import permutation items=['x','y'] result=list(permutations(items)) print(result)
22nd Nov 2022, 4:03 PM - 1
RESULT ---------------> 6
19th Aug 2020, 4:22 PM
Ali Boukhou - 1
6
7th Dec 2020, 5:30 AM
mt.ali - 1
6
25th May 2021, 6:28 PM - 1
question : What is the output of this code? program : from itertools import product a={1, 2} print(len(list(product(range(3), a)))) output : 6
28th May 2021, 5:50 PM   