You Are Here:

Community: Developer Discussion Boards

#1 Old Question Retreive dictionary objects in correct order... - 2007-02-13, 15:33

Join Date: Jan 2006
Posts: 64
Location: CPH, Denmark
madsbjoern's Avatar
madsbjoern
Offline
Regular Contributor
Hi

I've noticed a strange behavior in the dictionary objects - when I add three different keys, let's just say:

Code:
q={}
q[u'Image1']=u'path1'
q[u'XMLfile1']=u'path2'
q[u'Image2']=u'path3'
and print the keys:

Code:
q.keys()
it outputs the keys in alphabeticel order and not the order it was added:

[u'Image2', u'Image1', u'XMLfile1']

Does anyone know whether it possible to get the keys in the order it was added?
Reply With Quote

#2 Old Re: Retreive dictionary objects in correct order... - 2007-02-13, 16:12

Join Date: May 2004
Posts: 524
Location: Tampere, Finland
jethro.fn's Avatar
jethro.fn
Offline
Forum Nokia Champion
Quote:
Originally Posted by madsbjoern
Code:
q.keys()
it outputs the keys in alphabeticel order and not the order it was added:

[u'Image2', u'Image1', u'XMLfile1']

Does anyone know whether it possible to get the keys in the order it was added?
This is documented behaviour of the dictionary object, i.e. it will not preserve any order, alphabetical or otherwise. It was just a coincidence that you got your keys in alphabetical order.

From http://www.python.org/doc/2.2.2/lib/typesmapping.html:

Quote:
(3)

Keys and values are listed in random order. If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): "pairs = zip(a.values(), a.keys())". The same relationship holds for the iterkeys() and itervalues() methods: "pairs = zip(a.itervalues(), a.iterkeys())" provides the same value for pairs. Another way to create the same list is "pairs = [(v, k) for (k, v) in a.iteritems()]".
To preserve insertion order, insert the keys in an auxiliary list. You can even create your own type that does it automatically, by wrapping the built-in dictionary.
Reply With Quote

#3 Old Re: Retreive dictionary objects in correct order... - 2007-02-13, 16:24

Join Date: Feb 2005
Posts: 1,353
Location: Belgium (Europe)
cyke64's Avatar
cyke64
Offline
Super Contributor
Quote:
Originally Posted by madsbjoern
Hi

I've noticed a strange behavior in the dictionary objects - when I add three different keys, let's just say:

Code:
q={}
q[u'Image1']=u'path1'
q[u'XMLfile1']=u'path2'
q[u'Image2']=u'path3'
and print the keys:

Code:
q.keys()
it outputs the keys in alphabeticel order and not the order it was added:

[u'Image2', u'Image1', u'XMLfile1']

Does anyone know whether it possible to get the keys in the order it was added?
As jethro said to you it's the usual behavior of dictionnary in Python
But it exists implementation of ordered dictionary
I already put one link on it in the wiki
Look at odict !

Cyke64


pys60 1.4.5,1.9.7,pygame,PyS60 CE on E90 , N810 with Python 2.5.2 and ... last PyS60 1.9.7 with touch ui on 5800 !

pys60 extension modules on http://cyke64.googlepages.com/
Reply With Quote

#4 Old Re: Retreive dictionary objects in correct order... - 2007-02-13, 16:28

Join Date: Jan 2006
Posts: 64
Location: CPH, Denmark
madsbjoern's Avatar
madsbjoern
Offline
Regular Contributor
Quote:
Originally Posted by jethro.fn
To preserve insertion order, insert the keys in an auxiliary list. You can even create your own type that does it automatically, by wrapping the built-in dictionary.
Thanks - that's what I expected. Good idea with an auxiliary list, i'll solve it that way.

/Mads
Reply With Quote

#5 Old Re: Retreive dictionary objects in correct order... - 2007-02-13, 16:31

Join Date: Jan 2006
Posts: 64
Location: CPH, Denmark
madsbjoern's Avatar
madsbjoern
Offline
Regular Contributor
Quote:
Originally Posted by cyke64
As jethro said to you it's the usual behavior of dictionnary in Python
But it exists implementation of ordered dictionary
I already put one link on it in the wiki
Look at odict !

Cyke64
All right, i'll have a look
Reply With Quote

#6 Old Re: Retreive dictionary objects in correct order... - 2007-02-13, 18:41

Join Date: Jan 2007
Posts: 21
Location: Spain
Skarabeus
Offline
Registered User
Code:
>>>q={}
>>>q[u'Image1']=u'path1'
>>>q[u'XMLfile1']=u'path2'
>>>q[u'Image2']=u'path3'
>>>l=q.keys()
>>>l.sort()
>>>l
[u'Image1', u'Image2', u'XMLfile1']
Bye.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia