Dictionary from list?

Quinn Dunkan quinn at cruzeiro.ugcs.caltech.edu
Fri Oct 19 22:56:51 CEST 2001


On Fri, 19 Oct 2001 13:17:54 GMT, Jim Correia <correia at barebones.com> wrote:
>In article <u8ze8vwhh.fsf at python.net>, Michael Hudson <mwh at python.net> 
>wrote:
>
>> > I'm a python newbie.  I know how to do it "by hand" with a loop, but is 
>> > there a built in conversion operater that will let me do something 
>> > simply like the perl assignment?
>> 
>> No.  Write the loop.
>
>That's unfortunate - a simple assignment would be better.  In the 
>simplest case of my usage, the loop (while ultra short) could be 50% of 
>the code.

This doesn't make any sense to me.  If a two line loop is 50% of your program,
then your program is four lines.  If you're going to play meaningless
line-counting games at least come up with some more impressive numbers :)

And assignment that uses a heuristic to convert an array into a hash is not
really "simple".

>> I've never found myself needing to do this, but that may be because
>> I'm not used to having a convenient way of going from
>> [k1,v1,k2,v2,...] to a dict.
>
>The particular situation is the python implementation is going to be a 
>command line script.  The calling convention for this script is to pass 
>arguments on the command line in key value pairs.  The perl 
>implementation looks like (in the simplest case)
>
>my %args = @ARGV;
>
>foreach(keys %args)
>{
>   print("$_: $args{$_}\n");
>}
>
>And it would be called from the command line as
>
>perl myScript.pl name fred age 23 occupation "gravel worker"

import sys
d = {}
assert (len(sys.argv)-1) % 2 == 0, \
    'argv must consist of pairs, key "%s" has no value' % sys.argv[-1]
for i in range(1, len(sys.argv), 2):
    d[sys.argv[i]] = sys.argv[i+1]

for k, v in d.items():
    print k + ': ' + v

Note that the python version actually checks to make sure the input makes
sense.  There's no way to know how the perl version reacts to bad input except
by testing it or reading the documentation.  I suspect perl will silently give
the last key a nil value, but once again there's no way to know for sure by
just looking at it.  Hopefully you and everyone who reads your code has all
the little details in the camel book memorized.

This is a good demonstration of why many people prefer the explicit python
approach.  What if you want bad input to report an error?  What if you want it
to not be an error, but you want the default value to be something other than
nil?  What if you start off wanting it to be an error, but later decide it
should give a default value?  If you wrote this in perl using assignment,
you'd have to write a function and then track down all those assignments (have
fun checking every assignment in a large program) and replace them with a
function call.

Learning a new language involves more than learning the syntax and libraries.
Consider python an opportunity to gain another perspective on the practice of
programming.

>I'd like to implement the script with the same calling conventions in 
>python, and have an "easy" (typing wise, not conceptually, but I guess 
>I'll have to write a reusable function) way to take the arguments on the 
>command line and turn them into a dictionary.

A two line loop is pretty easy.  If you want to do this a lot then yes, you
should define a function, in which case 'd = dictconv(a)' is the same number
of lines than '%d = @a;'.  If you want to quibble, then yes, it's 7 characters
longer, but consider that you don't have to type '%@;' which reduces the
difference to 4 characters.  '%@' involves pressing shift twice, so that
further reduces it to a 2 character difference.  You could then name the
function dconv and type 1 *less* character!  Isn't python great?



More information about the Python-list mailing list