AttributeError: 'dict' object has no attribute (...) when iterate over a dictionary of sets

18,624

One of these things is not like the other:

>>> g1 = {0: set([1]), 1: set([0, 2]), 2: set([1, 3]), 3: set([2]), 4: {5, 6}, 5: {4}, 6: {}}
>>> for key in g1: 
...     print key, g1[key], type(g1[key])
...
0 set([1]) <type 'set'>
1 set([0, 2]) <type 'set'>
2 set([1, 3]) <type 'set'>
3 set([2]) <type 'set'>
4 set([5, 6]) <type 'set'>
5 set([4]) <type 'set'>
6 {} <type 'dict'>

{} is an empty dict, not the empty set. Use set() instead.

Share:
18,624

Related videos on Youtube

Juan David
Author by

Juan David

Updated on June 04, 2022

Comments

  • Juan David
    Juan David about 2 years

    I'm trying to remove a node from an undirected graph represented by his adjacency list but I'm receiving an error that I can't understand.

    Example of a ugraph:

    g1 = {0: set([1]), 1: set([0, 2]), 2: set([1, 3]), 3: set([2]), 4: {5, 6}, 5: {4}, 6: {}}
    

    Function that I'm creating:

    def remove_node(ugraph, node):
        """
        :param ugraph: An undirected graph
        :param node: The node that we want to remove
        :return: The undirected graph minus one node :)
        """
    
        try:
            del ugraph[node]
        except KeyError:
            print "The node is not in the graph" 
    
        for key in ugraph.keys():
            ugraph[key] = ugraph[key].difference(set([node]))
    
        return ugraph
    

    And this is my error:

    Traceback (most recent call last):
      File "/home/juan/PycharmProjects/AlgorithmicThinking_week2/resilence.py", line 46, in <module>
        print remove_node(g1, 1)
      File "/home/juan/PycharmProjects/AlgorithmicThinking_week2/resilence.py", line 42, in remove_node
        ugraph[key] = ugraph[key].difference(set([node]))
    AttributeError: 'dict' object has no attribute 'difference'
    

    Why is this happening?

    • Juan David
      Juan David about 9 years
      The print issue is for autocomplete. Please check my post again
    • Admin
      Admin about 9 years
      The error message is exactly what it says: upgraph is a standard Python dict object, and dict objects do not have an attribute (or method) like difference. Why do you think you need to call .difference on upgraph? If it were documented like that, there's something odd going on.
    • Admin
      Admin about 9 years
      Looking at your actual g1 object, I may spot the problem: the first few elements have values of type set, which do have a difference attribute. But the elements with key 6 has a value that is an (empty) dict, not a set.
    • Juan David
      Juan David about 9 years
      I'm not calling difference on ugraph but on the values of ugraph which are sets
    • Admin
      Admin about 9 years
      I noticed; DSM and Rahul found the same issue I found in the mean time.
  • Juan David
    Juan David about 9 years
    You are right but I dont understand why in some places I can use the set literal {} but in other places I need to use the set function call set([])
  • DSM
    DSM about 9 years
    Because {} was used to refer to the empty dictionary before there were set literals. (I wish the empty dict had been {:}, but can't be helped now.) So we're stuck with {} being the empty dictionary and {1} being a set.
  • Juan David
    Juan David about 9 years
    Thank you, my function is ok. The problem was in g1 as you said