0

Looking forward to suggestions using itertools

I have two lists of values for X and Y-axis

x=[75.0, 75.0, 74.5 ]
y=[34.0, 34.0, 33.5 ]

I want to know the points that I can obtain with these values, Don't need the combinations which starts with Y values and need to avoid the points due to duplicate values in x and y

the output that I am expecting

out= [(75,34),(75,33.5),(74.5,34),(74.5,33.5)]
yatu
  • 80,714
  • 11
  • 64
  • 111
VGB
  • 397
  • 3
  • 16
  • Does this answer your question? [combinations between two lists?](https://stackoverflow.com/questions/12935194/combinations-between-two-lists) – F Blanchet Jan 15 '20 at 10:15
  • 1
    Hi @VGB , don't forget you can upvote and accept answers. See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers), thanks! – yatu Jan 16 '20 at 21:01
  • I upvoted it!! somehow forget to accept the answer, Sorry bro!! You are the best! :D – VGB Jan 17 '20 at 10:48

3 Answers3

6

Use itertools.product:

from itertools import product

set(product(x,y))
# {(74.5, 33.5), (74.5, 34.0), (75.0, 33.5), (75.0, 34.0)}
yatu
  • 80,714
  • 11
  • 64
  • 111
1

What you need is itertools.product. I would also remove the duplicates from the original lists to avoid getting duplicates in the result.

from itertools import product


res = list(product(set(x), set(y)))

print(res)  # -> [(74.5, 33.5), (74.5, 34.0), (75.0, 33.5), (75.0, 34.0)]
Ma0
  • 14,712
  • 2
  • 33
  • 62
1

You can also try this using set comprehension

x=[75.0, 75.0, 74.5 ]
y=[34.0, 34.0, 33.5 ]

out = list({(i, j) for i in x for j in y})
Afnan Ashraf
  • 144
  • 13