I have this piece of python code
xy_tups = []
for x in ['m', 't', 'b']:
for y in ['b', 't', 'e']:
if x != y:
xy_tups.append ((x, y))
that outputs this: [('m', 'b'), ('m', 't'), ('m', 'e'), ('t', 'b'), ('t', 'e'), ('b', 't'), ('b', 'e')]
I need to create a list comprehension version of this piece of code but I am having trouble figuring it out. I have tried these methods
xy_tups = [x for x in ['m', 't', 'b'] and y for y in ['b', 't', 'e'] if x != y]
xy_tups = [x for y in ['m', 't', 'b'] and y for x in ['b', 't', 'e'] if x != y]
and I have tried adding the xy_tups.append(x,y)
to the list comprehension code but I get errors. I understand that each letter in the x
list is joined with each letter of the y
list once, but I cannot figure out how to put together the list comprehension.