[Tutor] Question on joining out of order dictionary elements

John Fouhy john at fouhy.net
Thu Jan 11 02:00:23 CET 2007


On 11/01/07, Andrew Robert <andrew.arobert at gmail.com> wrote:
> Only some of the key/value pairs in the dictionary are needed so a
> dump of all values does not work.
>
> Also, the order in which the values are joined is important so walking
> through and joining all values does not work either.

Well, a dictionary is by definition unordered, so you will need to
tell python which elements you want, and in which order.

I would do something like this:

####
config = {}
# insert code here to set the values of config

keysToDump = ['test1', 'test2', 'test3']
output = '\\'.join(config[k] for k in keysToDump)
####

(note that, if you are using python2.3 or earlier, you will need to
write that last line as:

output = '\\'.join([config[k] for k in keysToDump])

)

HTH!

-- 
John.


More information about the Tutor mailing list