+ 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
Dingo - avatar
18 Respuestas
+ 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
Hubert Dudek - avatar
+ 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
Hubert Dudek - avatar
+ 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
Sost - avatar
+ 5
6
11th Jan 2020, 7:26 AM
Babeetha Muruganantham
Babeetha Muruganantham - avatar
+ 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
Tyler Sandiford - avatar
+ 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
Dingo - avatar
+ 1
Thanks to Babeetha
30th Jun 2020, 7:24 AM
Faith Mundi
Faith Mundi - avatar
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
Dingo - avatar
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
makhan Kumbhkar - avatar
0
The ans is 6
30th Jun 2020, 7:24 AM
Faith Mundi
Faith Mundi - avatar
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
Pratap Vasava - avatar
0
from itertools import permutation items=['x','y'] result=list(permutations(items)) print(result)
22nd Nov 2022, 4:03 PM
Adike Sidhartha rao
Adike Sidhartha rao - avatar
- 1
RESULT ---------------> 6
19th Aug 2020, 4:22 PM
Ali Boukhou
Ali Boukhou - avatar
- 1
6
7th Dec 2020, 5:30 AM
mt.ali
mt.ali - avatar
- 1
6
25th May 2021, 6:28 PM
Hayder Jawad Al-ATBEE
Hayder Jawad Al-ATBEE - avatar
- 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
Madhavareddy
Madhavareddy - avatar
- 1
6 is the answer
24th Jul 2021, 6:14 AM
Judson Leo
Judson Leo - avatar
- 1
Ans 6
3rd Dec 2021, 2:30 PM
Thuwayba Ahmed
Thuwayba Ahmed - avatar