Two variable dictionary comprehension

Deborah Swanson python at deborahswanson.net
Tue Apr 4 19:10:16 EDT 2017


Sorry I bailed on you last night, but you know it's bad when you can't
read anymore.

I read through this today and saw several things I really need to work
with more, especially dicts, sets, generators and the zip function. I've
used all of them in fairly typical forms, but it's the atypical ones
that throw me. And strangely enough, I've never seen dicts described as
having key:value syntax, although it's certainly true and clarifies a
lot in thinking about them.

The one thing you mentioned that was completely new to me was the use of
an if clause in a list comprehension, and I can see there being times
when that's exactly what you want to do. I'll have to try the other
types of comprehensions and see if this same grammar works in all of
them. It should. Interesting how computer languages seem to be a hybrid
of math and linguistics.

I added all of these concepts and code snippets to my study list. Thanks
again Steve.  ;)

Deborah


Steve D'Aprano wrote, on Monday, April 03, 2017 6:05 PM
> 
> On Tue, 4 Apr 2017 03:27 am, Deborah Swanson wrote:
> 
> > I'll admit that both dictionaries and comprehensions are still a 
> > little bit fuzzy to me, especially when I get away from the common 
> > usages. This could be a good exercise to clarify some of the fuzzy 
> > areas.
> 
> 
> As far as comprehensions go, how is your mathematics?
> 
> If you remember your set builder notation from maths, list 
> comprehensions are based on that.
> 
> In maths, we say something like:
> 
> {2n + 1 : 0 ≤ n ≤ 9}
> 
> which might be read aloud as "the set of 2 times n, plus 1, 
> such that n is between 0 and 9 inclusive".
> 
> http://www.mathwords.com/s/set_builder_notation.htm
> 
> A similar notation might be:
> 
> {2n + 1 : n ∈ {1, 2, 3}}
> 
> said as "the set of 2 times n, plus 1, such that n is an 
> element of the set {1, 2, 3}".
> 
> 
> If you're a maths geek like me, this is second nature :-)
> 
> 
> Now, Python's list comprehension syntax is based on a similar 
> notation, except we spell it out in words rather than 
> symbols, using the familiar "for n in ..." syntax from 
> for-loops instead of : "such that".
> 
> {2*n + 1 for n in range(10)}
> 
> {2*n + 1 for n in (1, 2, 3)}
> 
> 
> A few other differences:
> 
> - list comprehensions use [ ] for the outermost brackets;
> - set and dict comprehensions use { };
> - generator expressions use ( ) (unless the parentheses can 
> be implied).
> 
> We're not restricted to mathematical expressions, and can use 
> any Python expression we like:
> 
> [value.method(arg)[1] for (value, arg) in zip(values, arguments)]
> 
> 
> translates roughly into this for-loop:
> 
> result = []
> for (value, arg) in zip(values, arguments):
>     result.append(value.method(arg)[1])
> 
> 
> 
> Another difference is that comprehensions allow an "if" clause:
> 
> [v.method(a)[1] for (v, a) in zip(values, arguments) if v is not None]
> 
> translates something like:
> 
> 
> result = []
> for (v, a) in zip(values, arguments):
>     if v is not None:
>         result.append(v.method(a)[1])
> 
> 
> There's more, but that covers probably 98% of comprehension usage.
> 
> And of course, remember that dict comprehensions use the same 
> key:value syntax as ordinary dict "literals" (the official 
> name is "dict display").
> 
> result = {}
> for key, value in zip(keys, values):
>     result[key] = value
> 
> 
> becomes
> 
> result = {key:value for (key, value) in zip(keys, values)}
> 
> although frankly that's better written as:
> 
> dict(zip(keys, values))
> 
> 
> 
> Hope this helps!
> 
> 
> 
> 
> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered 
> up, and sure enough, things got worse.
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list