473,395 Members | 1,422 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,395 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 1794
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.