473,699 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keep unique values between two dictionaries

50 New Member
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 1809
rhitam30111985
112 New Member
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 New Member
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
KaezarRex
52 New Member
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 New Member
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
1673
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
2587
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), username, date, and time field like this: 1234 williamstim 01AUG03 7:44:31 2348 williamstim 02AUG03 14:11:20
26
45420
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.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
7
7903
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 the keys from the old dictionaries? The keys in the new dictionary would be the keys from the old dictionary one (dict1) and the values in the new dictionary would be the keys from the old dictionary two (dict2). The keys would be joined by...
15
6715
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 that I can calculate weekly and monthly. At this point I have one entry per search phrase with the number of hits the search phrase has gotten, and the last time it was updated. As I start to take the program out of testing and move in more...
5
10213
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. This allows me to have one key with two values (one value in each dictionary). This is ugly code as the dictionaries could get out of synch (due to a bug or something). Titan
5
2871
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 from a previous function. The pseudo code for this problem would be: returnedlist = count = 0 for i in returnedlist: if count < len(returnedlist):
0
1379
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 value: a O(n) process. As your friend suggested, searching a dictionary requires O(1) time. A set is even better in this case, because you don't have any use for the values in the inner dictionary (sets and dictionaries are very similar in the...
10
1980
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, ++imanshu
0
8617
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8884
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7751
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.