RE: Dict Copy & Compare
- From: Steven D'Aprano <steve@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Apr 2007 20:15:41 +1000
On Mon, 30 Apr 2007 10:05:40 +0100, Robert Rawlins - Think Blue wrote:
I have two dicts, one named 'this' and the other named 'that'.
I want to get all the unique keys from 'this' and log them into a file, I
then want to take all the unique values from 'that' and log them into a
separate file.
The most straight-forward way is doing a simple pair of loops:
for key in this:
if key not in that:
logThis(key)
for key in that:
if key not in this:
logThat(key)
So it's just a case of firstly returning a list of all keys that are in
'this' but NOT in 'that' and then visa versa, then loop over them performing
the function.
Well, if you really do need to collect the list up front (why???) you can
do this:
uniqueFromThis = [key for key in this if key not in that]
uniqueFromThat = [key for key in that if key not in this]
Membership testing in dicts is efficient, so that should run quite fast
unless you have millions of keys common to both dictionaries.
--
Steven D'Aprano
.
- References:
- RE: Dict Copy & Compare
- From: Robert Rawlins - Think Blue
- RE: Dict Copy & Compare
- Prev by Date: Re: How do I parse a string to a tuple??
- Next by Date: Re: How do I parse a string to a tuple??
- Previous by thread: RE: Dict Copy & Compare
- Next by thread: Re: Dict Copy & Compare
- Index(es):
Relevant Pages
|