473,231 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 1787
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

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

Similar topics

1
by: python | last post by:
Hi- I have several different dictionaries. I want to make a unique list of all the keys in all the dictionaries. What would be the best way of doing that? Thanks.
7
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
7
by: ProvoWallis | last post by:
I'm still learning python so this might be a crazy question but I thought I would ask anyway. Can anyone tell me if it is possible to join two dictionaries together to create a new dictionary using...
15
by: l3vi | last post by:
I have a new system Im building that stores entries of what people are searching for on my sites. I want to be able to keep records of how many times a keyword was searched for daily, and from...
5
by: titan.nyquist | last post by:
Is there a typical way to create a dictionary (or hash table) with two values, instead of one? Currently, my data structure is TWO dictionaries, each with matching and fully sychronized keys. ...
5
by: Greg Corradini | last post by:
Hello All, I'm attempting to create multiple dictionaries at once, each with unique variable names. The number of dictionaries i need to create depends on the length of a list, which was returned...
0
by: Gabriel Genellina | last post by:
En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik <Shawn@Milochik.comescribió: A dictionary with keys is perfectly reasonable. But a *list* of values has to be searched linearly for every...
10
by: ++imanshu | last post by:
Hi, Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. Thanks,...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.