1
votes

What I am attempting to accomplish is to create a union of two dictionaries (consisting of single integers i.e. 1, 2, 3, 4, etc.) by taking the keys out of the dictionary, putting them into two lists, joining the two lists and then putting them back into a new dictionary that contains both lists. However, I am running into the

TypeError: unsupported operand type(s) for +: 
    'builtin_function_or_method' and 'builtin_function_or_method'

How would I get around this error?

Here are the relevant pieces of code.

class DictSet:
    def __init__(self, elements):
        self.newDict = {}
        for i in elements:
            self.newDict[i] = True

    def union(self, otherset):
        a = self.newDict.keys
        b = otherset.newDict.keys
        list1 = a + b
        new = DictSet(list1)
        return new

def main():
    allints = DictSet([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    odds = DictSet([1, 3, 5, 7, 9])
    evens = DictSet([2, 4, 6, 8, 10])
2
In future, please include a complete program in your question. It doesn't have to be long (in fact, the shorter the better!) but it must be complete. For an explanation of how asking such questions results in excellent answers, see How to Ask and especially minimal reproducible example.Robᵩ

2 Answers

2
votes

Why not use dict.update()?

def union(self, otherset):
    res = DictSet([])
    res.newDict = dict(self.newDict)
    res.newDict.update(otherset.newDict)
    return res
2
votes

You must call the keys() method. Try this:

    a = self.newDict.keys()
    b = otherset.newDict.keys()

EDIT: I see you are using Python3. In that case:

    a = list(self.newDict)
    b = list(otherset.newDict)