1

So , the problem is not in my code , it is in the output . Also , the problem is stated on Hacker Rank so if you know the solution to it, thanks but I do not need the solution to the original problem , I want my doubt to be cleared. So the problem is

You are given a two lists A and B . Your task is to compute their cartesian product X.

What I have done is ,

from itertools import product
list_A = list(map(int ,input().split()))
list_B = list(map(int ,input().split()))
x = list(product(list_A,list_B))
y = tuple(x)
print(y)

And this gives me the desired output : ((1, 3), (1, 4), (2, 3), (2, 4))

However , HackerRank doesn't seem to want a tuple . So I need to print out the same values , just not as a tuple .

Expected Output (1, 3) (1, 4) (2, 3) (2, 4)

I am sure that the solution to this must be pretty simple however I just can not get my head to it for some reason. All help is appreciated! Thanks!

5
  • Try printing just the list print(x) Commented Aug 22, 2020 at 11:26
  • That doesn't work either . It prints a list instead of a tuple. I used a tuple as the section on which I was working was "Tuples"> Thank you anyway. Commented Aug 22, 2020 at 11:28
  • Try print(' '.join([str(z) for z in x])) to get the string output Commented Aug 22, 2020 at 11:36
  • print(" ".join(f"{x}" for x in y)) Commented Aug 22, 2020 at 11:36
  • 2
    do print(*x) to pass the list elements as individual arguments to print Commented Aug 22, 2020 at 11:36

3 Answers 3

2

You can use str.join() to join a string version of each element with a space character between each:

x = product(list_A, list_B)
print(' '.join([str(t) for t in x]))

Update:

Also note some of the comments to your question have more direct answers, eg:

print(*product(list_A, list_B))
Sign up to request clarification or add additional context in comments.

Comments

0

Use for loop and print y one by one with end=' ' in print statement.

from itertools import product
list_A = list(map(int ,input().split()))
list_B = list(map(int ,input().split()))
x = list(product(list_A,list_B))
y = tuple(x)
for i in y:
    print(i,end=' ')

1 Comment

I ran this , the output is (1, 3)(1, 4)(2, 3)(2, 4) ; Anyway , thank you for your help. The problem has been solved!
0

I haven't been able to get the values to print out as one line. however, through I used this:

for i in range(len(y)):
    print(y[i])

to loop through and print level 0 values of the tuple.

1 Comment

You can use print([y[i] for i in range(len(y))])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.