for/ if against dict - one liner
Vincent Vande Vyvre
vincent.vande.vyvre at telenet.be
Tue Nov 14 02:24:28 EST 2017
Le 14/11/17 à 06:44, Andrew Z a écrit :
> 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
Maybe something like that:
lst = [do_magnificent_thing(dct[k]) for k in dct if k >= 2]
lst contains the returns of do_magnificent_thing(k) in unpredictable order.
Vincent
More information about the Python-list
mailing list