[Tutor] need a hint

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Wed Dec 4 12:14:02 CET 2013


Alan Gauld <alan.gauld <at> btinternet.com> writes:

> 
> On 04/12/13 10:22, Wolfgang Maier wrote:
> 
> >
> >          # instead of senateInfo[lastName] = state,
> >          # which builds a simple state dictionary
> >          if lastName in senateInfo:
> >              senateInfo[lastName].append((firstName, state))
> >          else:
> >              senateInfo[lastName] = [(firstName, state)]
> 
> Or replace those last four lines with
> 
> senateInfo[lastName] = senateInfo.get(
>                          lastName,[]).append((firstname,state))
> 

Right, forgot to mention your shorter alternative, Alan. Sorry about that.
Personally, I find the four-liner easier to explain (specially to
beginners), but I guess that's a matter of style and, of course, the
one-liner introduces the generally useful get method.
Wolfgang




More information about the Tutor mailing list