471,354 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

keep unique values between two dictionaries

50
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.

Expand|Select|Wrap|Line Numbers
  1. def filterByKey(dict1, dict2):
  2.     '''
  3.     Takes two dictionaries and deletes
  4.         matching records;
  5.     Dict1 is the main dictionary;
  6.     Dict2 is the secondary dictionary.
  7.     RETURNS: dictionary Dict1 of unique
  8.         values.
  9.     '''
  10.     for key in dict2:
  11.         if key in dict1.keys():
  12.             del dict1[key]
  13.  
  14.     return dict1
Cheers
Sep 17 '07 #1
4 1608
rhitam30111985
112 100+
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.

Expand|Select|Wrap|Line Numbers
  1. def filterByKey(dict1, dict2):
  2.     '''
  3.     Takes two dictionaries and deletes
  4.         matching records;
  5.     Dict1 is the main dictionary;
  6.     Dict2 is the secondary dictionary.
  7.     RETURNS: dictionary Dict1 of unique
  8.         values.
  9.     '''
  10.     for key in dict2:
  11.         if key in dict1.keys():
  12.             del dict1[key]
  13.  
  14.     return dict1
Cheers
del dict1[key] will delete the values .. not the key..
u need to do this:

Expand|Select|Wrap|Line Numbers
  1.  for key in dict2:
  2.         if key in dict1.keys():
  3.             del key
  4. dict1=dict2 #since the updated dictionary is contained in dict2 at this  point
  5. return dict1
  6.  
Sep 17 '07 #2
rhitam30111985
112 100+
i think above solution is wrong...
this shud do the trick:
Expand|Select|Wrap|Line Numbers
  1.  
  2. for key in dict2:
  3.                if key in dict1.keys():
  4.                     dict1.pop(key)
  5.  
  6.  
  7. return dict1
  8.  
Sep 17 '07 #3
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.

Expand|Select|Wrap|Line Numbers
  1. def filterByKey(dict1, dict2):
  2.     '''
  3.     Takes two dictionaries and deletes
  4.         matching records;
  5.     Dict1 is the main dictionary;
  6.     Dict2 is the secondary dictionary.
  7.     RETURNS: dictionary Dict1 of unique
  8.         values.
  9.     '''
  10.     for key in dict2:
  11.         if key in dict1.keys():
  12.             del dict1[key]
  13.  
  14.     return dict1
Cheers
Try it this way:
Expand|Select|Wrap|Line Numbers
  1. for key in dict2.keys():
  2.     if key in dict1.keys():
  3.         del dict1[key]
  4.         del dict2[key]
  5.     return [dict1, dict2]
  6.  
That way your iterating over a list of the keys that dict2 contained when you called the function, but not the actual dictionary.
Sep 17 '07 #4
kdt
50
Thanks guys,

It's working now. Iterating over the dictionary keys solved the problems - so thanks again.
Sep 17 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by python | last post: by
7 posts views Thread by Nova's Taylor | last post: by
26 posts views Thread by Agoston Bejo | last post: by
7 posts views Thread by ProvoWallis | last post: by
5 posts views Thread by titan.nyquist | last post: by
5 posts views Thread by Greg Corradini | last post: by
reply views Thread by Gabriel Genellina | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.