473,748 Members | 6,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Keys order in dictionaries


Is there any order in dictionaries that will never change ? I've noticed that
assigning always the same elements to the dict puts them always in the same
order. Like regexp which takes 3 values, 'year', 'month', 'day' - I always
get this order:

'year': .., 'day':.., 'month':..

No idea why this order tho.. :) Is there any philosophy in this ?
And if I add only one new value to this regexp, so that it takes 4 now the
order changes totally, but still remains the same for those 4 particular
key-names. If I add 'id' to the previous dict I get:

'year': .., 'id':.., 'day':.., 'month':..

And every time regexp matches those values order is still the same.
Can somebody explain this matter or give some urls to pages where I can
read about this.
And the second thing that bothers me is assigning multiple values at once
from dictionary. I mean:

x, y, z = dict # where dict = { 'xx':.., 'yy':.., 'zz':.. }

This works okay for me, if I know that dict is always of size 3 and there
is the same order of keys:values.

But now I came to the point when this dict can have both - 3 or 4 items
(same as this example from above, 'id' is the additional item that is not
always present). And I want to assign x, y, z like I did before, but ommit
id value. Is it possible to do at once, with some multiple assignment ?
Maybe there is a way to specify what three items should be taken from dict
and assigned to x, y, z? I know, I can always do something like:
x = dict['xx']
y = dict['yy'] and so on, but I'm just curious if some more complicated
multiple assignment is possible in Python.

How would you solve this problem?

/Vald

Jul 18 '05 #1
2 7063
On Fri, 27 Jun 2003 01:26:16 +0200, Brainwashed wrote:
Is there any order in dictionaries that will never change ?


No. The specification for the built-in dictionary type specifically
disclaims any preservation of order, to allow the implementation of any
arbitrary dictionary algorithm, with different, unpredictable (to the
user) storage and retrieval orders.

You can get ordered dictionaries (see the Python cookbook article
already pointed to), but not by directly using the built-in dictionary
type.

--
\ "I have yet to see any problem, however complicated, which, |
`\ when you looked at it in the right way, did not become still |
_o__) more complicated." -- Paul Anderson |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
Jul 18 '05 #2
Brainwashed <va**@valis.amb er.eu.org> wrote in
news:ma******** *************** **********@pyth on.org:
Is there any order in dictionaries that will never change ? I've
noticed that assigning always the same elements to the dict puts them
always in the same order. Like regexp which takes 3 values, 'year',
'month', 'day' - I always get this order:

'year': .., 'day':.., 'month':..

No idea why this order tho.. :) Is there any philosophy in this ?


Just because you never saw the order change doesn't mean it won't change.
The code below shows just how easy it is to force the order to change:
d = { 'year': 1, 'month': 2, 'day': 3 }
d {'month': 2, 'day': 3, 'year': 1} for i in range(30): d[i] = i for i in range(30): del d[i] d {'month': 2, 'year': 1, 'day': 3} dict(d) {'year': 1, 'day': 3, 'month': 2}


(BTW, that last one surprised me, I had expected dict(d) simply to restore
the original order instead of producing a third ordering).

--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #3

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

Similar topics

1
1674
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.
3
1707
by: Blair Hall | last post by:
I would like to determine whether two dictionaries have the same set of keys. Can anyone tell me if I HAVE to sort the key sequences as in this code snippet: # d1, d2 already created k1 = d1.keys() k1.sort() k2 = d2.keys() k2.sort()
90
10804
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in future Python versions and let dict.keys() and dict.values() both return sets instead of lists. If d is a dict, code like: for x in d.keys():
7
7906
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 the keys from the old dictionaries? The keys in the new dictionary would be the keys from the old dictionary one (dict1) and the values in the new dictionary would be the keys from the old dictionary two (dict2). The keys would be joined by...
14
3465
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the dictionary keys and values to be sets. Presently {1:0,2:0,3:0}.keys() will produce but it could also...
22
2364
by: bearophileHUGS | last post by:
>From this interesting blog entry by Lawrence Oluyede: http://www.oluyede.org/blog/2006/07/05/europython-day-2/ and the Py3.0 PEPs, I think the people working on Py3.0 are doing a good job, I am not expert enough (so I don't post this on the Py3.0 mailing list), but I agree with most of the things they are agreeing to. Few notes: - input() vanishes and raw_input() becomes sys.stdin.readline(). I think a child that is learning to program...
24
4307
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to these keys -- I only need to be able to retrieve the value associated with a certain key, so I do not want to have the keys stored in memory. Could I just hash() the url strings first and use the resulting integer as the key? I think what I'm...
11
11413
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the keys are numeric, the keys themselves are ordered in the dictionary. part of my code is like this: radix={} for i in range(256):
10
1982
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, ++imanshu
0
8989
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, 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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
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
9367
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
9319
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
9243
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 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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4599
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
3309
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
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.