+4 votes
in Programming Languages by (77.2k points)
I want to combine elements of two python lists to create a list of tuples. The elements should not be equal. What is the Pythonic way?

1 Answer

+4 votes
by (354k points)
selected by
 
Best answer

You can try the list comprehension.

Here is an example:

>>> x=[1,2,3]
>>> y=[11,12,3,4]
>>> [(i,j) for i in x for j in y if i!=j]
[(1, 11), (1, 12), (1, 3), (1, 4), (2, 11), (2, 12), (2, 3), (2, 4), (3, 11), (3, 12), (3, 4)]
>>>


...