Two variable dictionary comprehension

Deborah Swanson python at deborahswanson.net
Tue Apr 4 01:53:15 EDT 2017


Gregory Ewing wrote, on Monday, April 03, 2017 4:23 PM
> 
> Deborah Swanson wrote:
> > All my guesses were based on the
> > single variable (the most common type) examples I found. I just
didn't 
> > think of putting a colon after 'label', and found nothing to suggest

> > that's what I should do.
> 
> Hmmm, I'm not sure what the docs could do to make that any 
> clearer. The key:value syntax is part of *every* dict 
> comprehension (it's the only thing that distinguishes a dict 
> comprehension from a set comprehension).

I guess I saw a lot of examples that weren't clearly using the key:value
syntax, and all of it was so unfamiliar, that pattern just didn't stand
out to me. But it's starting to now, and that's the direction I need to
go in for dict comprehensions.

> > Despite my earlier words and protestations that I did look for two 
> > variable dict comprehensions, fairly diligently, I am taking what
you 
> > said seriously.
> 
> Don't feel too bad. Sometimes it's hard to know what to 
> google for, even for experienced people! Also it's hard for 
> doc writers to anticipate how less experienced people will 
> think. It wouldn't have occurred to me to use the phrase "two 
> variable dict comprehension" when writing documentation.

Yes, I was pretty sure the terminology I phrased the question with
wasn't correct, but I didn't know the right words to say, or where to
look them up, so I just tried to be as descriptive as I could.

But I accomplished my purpose in asking the question, even if it was
poorly put, and I've received quite a bit of good information, the
terminology I couldn't put my finger on, and some solid pointers of
directions to go in the future.
 
> Another thing it's important to be able to do is see through
> to the principles behind things, and then use those 
> principles to solve new problems that aren't explicitly 
> covered in any docs or examples.

Ah, yes, there's the rub. But it's true, from cooking to car mechanics
to Python coding, the key is in becoming familiar with the subject, and
hands on is the best teacher. (Yes, yes, we could have a discussion
about the role of education, but I'll say that it's seeing it all in
motion for yourself many times that seals the teaching into something
you know and don't need to be told.)

> The general principle behind all the forms of comprehension
> is that they consist of a prototypical element, followed by 
> some clauses that generate values for that element.
> 
> The syntax for the prototypical element mirrors that of the 
> corresponding display. So, for a dict comprehension, the 
> prototypical element has the form 'key:value', where 'key' 
> and 'value' are arbitrary expressions. From there, it's just 
> a matter of figuring out what to put into those expressions.
> 
> In your case, another part of the puzzle is the fact that
> you can unpack a tuple obtained from a for-loop into
> variables. That's a general feature of for-loops, both
> inside and outside of comprehensions, so you probably
> won't find it mentioned explicitly under the heading of
> dict comprehensions.
> 
> > Maybe it would be worthwhile to scrape the whole mess and have it in

> > searchable text form.
> 
> The HTML Python docs have a "Search" box, although I haven't 
> really used it so I don't know how well it works. 

It sucks, in a phrase. You're presented with a list of web page titles,
very few of which seem to have much to do with the Python language topic
you type into the search box. If you're interested in theoretical
dissertations you'll be pleased, but if you're looking for barebones
mechanical descriptions of the language you'll be wasting your time, in
my experience.

> In my 
> experience, Google with a site search often works a lot 
> better than search functions provided by sites themselves.

Yes, I used to use Google's site search on a regular basis but somehow
got out of the habit. It likely can't be beat if you know which website
will have the answer you're looking for, but the old Google truly shown
at finding places you didn't already know about.

> > I hope you won't be miffed though if I still come up empty handed
and 
> > come back here to ask again.
> > 
> > Deborah
> > 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list