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
  • 86,083
  • 12
  • 84
  • 139
VGB
  • 447
  • 6
  • 21

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
  • 86,083
  • 12
  • 84
  • 139
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
  • 15,057
  • 4
  • 35
  • 65
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
  • 174
  • 1
  • 14