0

I was trying to build a dictionary with recursion for a school project. Right now I think I have the general structure figured out, but I can't figure out how to get the return statement to concatenate pieces of the dictionary together.

I realize this would probably be easier by constructing an empty dictionary then adding to it, but I wanted to see if there were any tricks I could use.

The output I was looking for is:

print(recur_join([1,2,3], ['a', 'b', 'c']))
>>> {1: 'a', 2 : 'b', 3 : 'c'}

I have tried .update() and something of the form dict(basket_one, **basket_two) from another answer. I may have used them wrong. I am using python 3.

Here is my code as of now:

def recur_join(list1, list2):

    if len(list1) == len(list2):

        if len(list1) == 1:
            return {list1[0]: list2[0]}

        elif len(list1) > 1:
            # dict(basket_one, **basket_two)
            #return dict({list1[0]: list2[0]}, **recur_join(list1[1:], 
    list2[1:]))
            return {list1[0]: list2[0]}.update(recur_join(list1[1:], list2[1:]))

    else:
        print('lists do not match in size')
        return 0

Any help would be appreciated, sorry if this was answered before. Thanks!

0

2 Answers 2

2

I suggest you don't use recursion and use dict comprehensions instead:

def recur_join(list1, list2):
    if len(list1) != len(list2):
        print('lists do not match in size')
        return
    else: return {list1[i]:list2[i] for i in range(len(list1))}

For the recursive route (warning: very ugly):

def recur_join(list1, list2):
    if len(list1) != len(list2):
        print('lists do not match in size')
        return
    elif list1 == [] and list2 == []:
        return {}
    else:
        return dict(list({list1[0]: list2[0]}.items()) + list(recur_join(list1[1:], list2[1:]).items()))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was using recursion as more of a puzzle and because I had to write a program with recursion for a class, Thanks though. I will keep that dict comprehension in mind!
1

"Cleanish" recursive solution. I would personally use Primusa's dictionary comprehension solution.

def recur_join(list1, list2):
    cur = {}
    if len(list1) == len(list2) and len(list1) > 0:
        cur = {list1[0]: list2[0]}
        if len(list1) > 1:
            cur.update(recur_join(list1[1:], list2[1:]))
    else:
        print('lists do not match in size')
    return cur

2 Comments

I picked yours because I was looking specifically for the recursive option, the dict comprehension looks far cleaner, but I wanted to see how to do it with recursion for fun (and my class.)
If you are interested in developing a knack for recursive solutions I highly recommend picking up Scheme (or Lisp), it is an interestingly fun language!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.