Dictionary from list?

Jim Correia jim.correia at pobox.com
Sat Oct 20 03:14:46 CEST 2001


In article <slrn9t14si.o5o.quinn at cruzeiro.ugcs.caltech.edu>,
 quinn at cruzeiro.ugcs.caltech.edu (Quinn Dunkan) wrote:

> 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 :)

It was a stupid argument.

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

I'm used to the perl syntax for this operation, and it would be easier 
to type.  That is all.

> 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

That's what I ended up doing.

> 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.

It fails at runtime with an odd number of elements in hash list error.

> Hopefully you and everyone who reads your code has all
> the little details in the camel book memorized.

I was just asking if there was a built in coercion available.  
(Coercions aren't unheard of in scripting languages.)  This wasn't a my 
language is better than your language war.  I don't have particularly 
strong feelings about perl or python one way or the other (I do most of 
my work in compiled languages - C mostly) but use the right tool, or 
sometimes the convenient tool, for the job.

> This is a good demonstration of why many people prefer the explicit python
> approach.  What if you want bad input to report an error?

There is nothing stopping you from checking and doing that in perl 
either.

> 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?  

I don't see anything stopping you from doing this in perl (or anything 
else for that matter).

> 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.

But you are ignoring the initial constraints :-).  In this particular 
situation the arguments are passed to the script as key/value pairs.  
The conversion is done at the top of "main" and the dictionary is used 
throughout the rest of the program.

> 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.

No need to be condescending.  I've got plenty of "practice" and 
experience programming as well as shipping large, complex, high quality 
products to customers.  I didn't ask the question so you can start a 
pissing contest of credentials.

> 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.

I was looking for the perl conversion because it was easier to type, and 
I didn't have to carry around an extra conversion function (either in an 
external file or by cutting and pasting into otherwise single, one file, 
portable scripts).  That's why a language intrinsic was desired.  Since 
there isn't one, the two line loop will have to suffice.



More information about the Python-list mailing list