keep unique values between two dictionaries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdt
    New Member
    • Mar 2007
    • 50

    keep unique values between two dictionaries

    Hi

    Trying to create a function that takes two dictionaries, and deletes key:values that are common in both dictionaries. So far I have the following; but I can only delete values in one dictionary as I am iterating over the other. Or is there a way to rename keys in dictionaries? Thanks in advance.

    Code:
    def filterByKey(dict1, dict2):
        '''
        Takes two dictionaries and deletes
            matching records;
        Dict1 is the main dictionary;
        Dict2 is the secondary dictionary.
        RETURNS: dictionary Dict1 of unique
            values.
        '''
        for key in dict2:
            if key in dict1.keys():
                del dict1[key]
                
        return dict1
    Cheers
    Last edited by kdt; Sep 17 '07, 12:11 PM. Reason: another question
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    #2
    Originally posted by kdt
    Hi

    Trying to create a function that takes two dictionaries, and deletes key:values that are common in both dictionaries. So far I have the following; but I can only delete values in one dictionary as I am iterating over the other. Or is there a way to rename keys in dictionaries? Thanks in advance.

    Code:
    def filterByKey(dict1, dict2):
        '''
        Takes two dictionaries and deletes
            matching records;
        Dict1 is the main dictionary;
        Dict2 is the secondary dictionary.
        RETURNS: dictionary Dict1 of unique
            values.
        '''
        for key in dict2:
            if key in dict1.keys():
                del dict1[key]
                
        return dict1
    Cheers
    del dict1[key] will delete the values .. not the key..
    u need to do this:

    [CODE=python]
    for key in dict2:
    if key in dict1.keys():
    del key
    dict1=dict2 #since the updated dictionary is contained in dict2 at this point
    return dict1
    [/CODE]

    Comment

    • rhitam30111985
      New Member
      • Aug 2007
      • 112

      #3
      i think above solution is wrong...
      this shud do the trick:
      [CODE=python]
      for key in dict2:
      if key in dict1.keys():
      dict1.pop(key)


      return dict1
      [/CODE]

      Comment

      • KaezarRex
        New Member
        • Sep 2007
        • 52

        #4
        Originally posted by kdt
        Hi

        Trying to create a function that takes two dictionaries, and deletes key:values that are common in both dictionaries. So far I have the following; but I can only delete values in one dictionary as I am iterating over the other. Or is there a way to rename keys in dictionaries? Thanks in advance.

        Code:
        def filterByKey(dict1, dict2):
            '''
            Takes two dictionaries and deletes
                matching records;
            Dict1 is the main dictionary;
            Dict2 is the secondary dictionary.
            RETURNS: dictionary Dict1 of unique
                values.
            '''
            for key in dict2:
                if key in dict1.keys():
                    del dict1[key]
                    
            return dict1
        Cheers
        Try it this way:
        [CODE=python]for key in dict2.keys():
        if key in dict1.keys():
        del dict1[key]
        del dict2[key]
        return [dict1, dict2]
        [/CODE]

        That way your iterating over a list of the keys that dict2 contained when you called the function, but not the actual dictionary.

        Comment

        • kdt
          New Member
          • Mar 2007
          • 50

          #5
          Thanks guys,

          It's working now. Iterating over the dictionary keys solved the problems - so thanks again.

          Comment

          Working...