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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Python
  3. Itertools
  4. itertools.product()
  5. Discussions

itertools.product()

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 496 Discussions, By:

votes

Please Login in order to post a comment

  • alanlunspace
    6 years ago+ 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.

    130|
    Permalink
    View more Comments..
  • gbryan
    6 years ago+ 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.

    16|
    Permalink
  • marinskiy
    4 years ago+ 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 :)

    5|
    Permalink
  • coder_aky
    3 years ago+ 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()))))
    
    3|
    Permalink
  • pralay_daz
    6 years ago+ 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,
    
    2|
    Permalink
    View more Comments..
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature