473,545 Members | 1,638 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:


is silent on the subject. As for:


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 1476
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


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 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

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?
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 = {}
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
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...
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{}
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
by: cnb | last post by:
Are dictionaries the same as hashtables?
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__
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={}
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
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. ...
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...
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...
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...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
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...
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
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...

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.