Dipak Tripathi
Dipak Tripathi

Reputation: 1

Formatting the output in python

I have tried this approach:

print((i, j) for i in [0,x] for j in [0, y] if (i+j)!=n) 

where x,y,n are integers. Formatting the output in python from [(1,2) , (2,3)] to [[1,2], [2,3]]

Upvotes: 0

Views: 65

Answers (2)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160367

And you're getting output of <generator object at ...>, because that's the only argument you supply the generator expression. Either turn it into a list comp and unpack it to print:

print(*[(i, j) for i in [0,x] for j in [0, y] if (i+j)!=n])

or create the generator first, iterate through it and print out its results.

Upvotes: 0

Drathier
Drathier

Reputation: 14509

Simply change ( to [ (and wrap it in a list constructor to print the list, rather than the generator).

print(list([i, j] for i in [0,x] for j in [0, y] if (i+j)!=n))

Upvotes: 1

Related Questions