issues with searching through dictionaries for certain values

Steve Holden steve at holdenweb.com
Fri Feb 1 10:32:30 EST 2008


Connolly wrote:
> Hey,
> 
> Right basically I've got to the end of my main section of my program an I've 
> got it comparing the same dictionary to ensure that the values are the same 
> (sounds stupid I know), yet what my line of code I am using to do this is 
> failing to do is to check every single value. It is only checking the final 
> value in my dictionary, if you require more info than this feel free to say 
> but any help would be appreciated.
> Code in use is displayed below.
> 
> f4 = files_stored[0]
> f5 = files_stored[0]
> 
> for key in f4:
>     for items in f5:
>         if key == items:
>             f6 = {start+1: key}
>             final_dict.update(f6)
>         else:
>             print key, 'false'
> 
> print final_dict
>  
> 
> 
Some points.

1: f4 and f5 are both references to the *same* dictionary, as you can 
see by comparing id(f4) and id(f5).

2: Suppose you *did* have different dictionaries, if you want to test 
that they have the same values associated with the same keys this 
condition is known as "equality", and can be tested for with

     f4 == f5

3: You seem to believe that you are iterating over the keys of f4 and 
the items of f5, or perhaps you are just bad at choosing variable names.

4: The best way to write

     f6 = {start+1: key}
     final_dict.update(f6)

    is normally

     final_dict[start+1] = key

5: I presume start is set somewhere before this code, avoiding the 
NameError or AttributeError. It's hard to see whether you want the same 
value for all uses of start, but if you don't then you might want to 
consider incrementing start after you use it.

6: What do you *really* want to do?

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list