We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Itertools
- itertools.product()
- Discussions
itertools.product()
itertools.product()
+ 26 comments My Python 3 version:
from itertools import product a = list(map(int, input().split())) b = list(map(int, input().split())) print(*product(a, b))
Time flies. Answered this 3 years ago just like yesterday...
About the unpack operator
*
in*product(a, b)
, please kindly refer to Expression lists|Python Documentation and it further refers to PEP 448 with clear examples. For dictionary, the unpacker operator is**
instead. Thanks for the great Python.>>> print(*[1], *[2], 3) 1 2 3 >>> dict(**{'x': 1}, y=2, **{'z': 3}) {'x': 1, 'y': 2, 'z': 3}
The list function for a and b is excessive. Ihor_L's version is simpler.
+ 1 comment Yes, the solution is pretty simple, but the Editorial answer is a paragon of readability. Unless the question asks for it, long one-line answers should be verboten.
+ 3 comments Here is Python 3 solution from my HackerrankPractice repository:
from itertools import product a = list(map(int, input().split())) b = list(map(int, input().split())) print(*list(product(a, b)))
Feel free to ask if you have any questions :)
+ 1 comment 100% working ,Simple and easy :-)
Here's in Python 3
from itertools import product print(*product(list(map(int, input().split())), list(map(int, input().split()))))
+ 4 comments In python 2:
from itertools import product A = map(int,raw_input().split()) B = map(int,raw_input().split()) A.sort() B.sort() ans = [A,B] AxB = list(product(*ans)) for i in AxB: print i,
Load more conversations
Sort 496 Discussions, By:
Please Login in order to post a comment