for/ if against dict - one liner

Andrew Z formisc at gmail.com
Sat Nov 18 14:54:49 EST 2017


Gentlemen,
 thank you very much for the replies and help.

Vincent's solution worked perfectly for me.
Alister - you are correct and for me, i was looking to achieve that -
readability and slightly less keystrokes.

On Tue, Nov 14, 2017 at 4:59 AM, alister via Python-list <
python-list at python.org> wrote:

> On Tue, 14 Nov 2017 00:44:18 -0500, Andrew Z wrote:
>
> > Hello,
> >  i wonder how do i get the "for" and "if" to work against a dictionary
> >  in
> > one line?
> >
> > basically i want to "squeeze":
> >  dct= [ 1 : "one", 2:"two", 3:"three"]
> >  for k, val in dct:
> >    if k >= 2:
> >       # do magnificent things
> >
> > Thank you AZ
>
> why the need to single line it?
> is your computer running out of new line & space characters?
> it probably could be done with a dictionary comprehension but will that
> make things easier or harder to read/understand?
>
> "Readability counts"
>
>
>
>
> --
> (Presuming for the sake of argument that it's even *possible* to design
> better code in Perl than in C.  :-)
>     -- Larry Wall on core code vs. module code design
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list