I need to create a function that takes two lists as arguments and returns a list of the pairs of the elements in the two lists using recursion in python 3.x.
The input create_all_pairs([1,2], [3,4])
should give me :
[(1,3), (1,4), (2,3), (2,4)]
.
I have created this function in 3 differen ways: using for-loops, using while-loops and using list comprehension.
def create_all_pairs_for(xs, ys):
lst = []
for x in xs:
for y in ys:
lst.append((x,y))
return lst
def create_all_pairs_while(xs, ys):
lst = []
xscount = 0
yscount = 0
while xscount < len(xs):
while yscount < len(ys):
lst.append((xs[xscount], ys[yscount]))
yscount += 1
xscount += 1
yscount = 0
return lst
def create_all_pairs_listcomp(xs, ys):
lst = [(a,b) for a in xs for b in ys]
return lst
How can i write this function using recursion? This is what i have got so far, but i feel completely lost.
def create_all_pairs_rec(xs, ys):
if not xs:
return []
else:
return list(map(create_all_pairs_rec(xs, ys)), ys)