473,778 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing dictionaries, is this valid Python?

Hi, people.

I noticed today that dictionaries seem to support `==' comparison.
(Retrospectivel y, it is strange that I never needed it before! :-)
Yet, before relying on this, I seeked for confirmation in the Python
manuals, and did not succeed in finding it. In particular:

http://www.python.org/doc/2.3.5/lib/typesmapping.html

is silent on the subject. As for:

http://www.python.org/doc/2.3.5/lib/comparisons.html

it only says "Comparison operations are supported by all objects", which
is a little vague, and no promise that comparisons are meaningful (for
example, one might wonder what would exactly mean the comparison of open
files). The node even says: "Two more operations with the same
syntactic priority, "in" and "not in", are supported only by sequence
types (below).", which suggests that the information might not be fully
up-to-date, at least regarding dictionaries.

Would someone know where I could find a confirmation that comparing
dictionaries with `==' has the meaning one would expect (even this is
debatable!), that is, same set of keys, and for each key, same values?

--
François Pinard http://pinard.progiciels-bpi.ca
Dec 14 '05 #1
1 1488
François Pinard <pi****@iro.umo ntreal.ca> writes:
Would someone know where I could find a confirmation that comparing
dictionaries with `==' has the meaning one would expect (even this is
debatable!), that is, same set of keys, and for each key, same values?


It may not exist, so you'll have to go look at the source. That sure
looks like it does what you expect.

However, you should know that *any* to objects in python can be
compared for equality. The default behavior is to check to see if they
are "the same" object. If some class or type does anything else, it'll
have code to do the comparison. You can check the latter behavior
yourself pretty easily:
a = dict(a = 1)
b = dict(a = 1)
a is b False a == b

True

So dictionaries have their own comparison code. I can't think of much
else it could be but what you suggest.

Now, what asking if one dictionary is less than another means, there
you're on your own (but that's in the source as well).

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
8658
by: Kill Bill | last post by:
type(i) == "<type 'float'>" this always returns false. How come? type(i)returns <type 'float'> if i is a float so why isn't == working?
8
2623
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
3
2376
by: Shivram U | last post by:
Hi, I want to store dictionaries on disk. I had a look at a few modules like bsddb, shelve etc. However would it be possible for me to do the following hash = where the key is an int and not a string bsddb requires that both the key,value are string. shelve does support values being object but not the keys. Is there any
210
10556
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again I am missing that feature. Maybe there is something wrong with my programming style, but I rather think it is generally useful. I fully agree with the following posting where somebody complains why so very basic and useful things are not part...
7
1386
by: TYR | last post by:
I'd like to do something like this; iterate through a file which consists of data stored in dictionary format, one dict on each line, and read each line into a new dict using one of the values in the dict as its name... for example: stuff = open('data.txt') for eachLine in stuff: name{}
1
165
by: Edwin.Madari | last post by:
by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after copy.deepcopy stmt in this example.. bar = 0 and re-run.... fun learning with python... Edwin -----Original Message----- From: Madari, Edwin Sent: Thursday, August 14, 2008 9:24 PM
14
1824
by: cnb | last post by:
Are dictionaries the same as hashtables?
6
3711
by: Terry Reedy | last post by:
Gerhard Häring wrote: The new fractions module acts differently, which is to say, as most would want. True Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> F(1.0) File "C:\Program Files\Python30\lib\fractions.py", line 97, in __new__
4
5969
by: Gilles Ganault | last post by:
Hello I fill two dictionaries with the same number of keys, and then need to compare the value for each key, eg. #Pour chaque APE, comparaison societe.ape.nombre et verif.ape.nombre import apsw #============ dic1={}
0
9464
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
10292
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
10122
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
10061
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
8954
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
6722
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
5368
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...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.