[Tutor] Question on joining out of order dictionary elements

raghu raghu vgvr620034 at gmail.com
Thu Jan 11 11:45:44 CET 2007


Hi everyone,

I have a quick quick question joining out of order dictionary values.

For example:

I created an empty

>>> config={}

Added some key/value pairs

>>> config["test1"]="elem1"
>>> config["test2"]="elem2"
>>> config["test3"]="elem3"
....
 etc
>>>

Dumped the values and joined them at the same time.

>>> print "\\".join(config.values())
elem1\elem3\elem2

This is fine but it doesn't entirely solve the issue.

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.


The simplest way would be to do something like:

>>> print "\\".join((config["val2"],config["val1"],config["val3"]))
elem2\elem1\elem3

or

>>> print "%s\\%s\\%s" % (config["val2"],config["val1"],config["val3"])
elem2\elem1\elem3

but this seems somehow uneligent.

Are there a more efficient/compact ways of doing this kind of
operation or is this it?

The key/value pairs in these examples are contrived for purposes of
this discussion but the end goal is to piece together server and
directory path information for use with pysvn.

I have a Perl programmer who is learning Python and he is griping that
this kind of operation is far simpler in Perl.

To print or to retain individual values from a list,it has to be written in
the form of
config={'test1':'elem1','test2':'elem2','test3':'elem3'}
config['test4'] = 'elem4'
print config.values()
print config['test1']- To get individual values from a list
If we want to retrieve all the values or say 2 from 4 its not possible to
put in a while loop and get that as a list only takes one argument at a time
better way is to extract individually and respective keys could not be
obtained if corresponding values are given.



-- 

                                           Vanam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070111/8fbee283/attachment.html 


More information about the Tutor mailing list