On 10/8/17 1:00 AM, Andrew Z wrote:
and how about adding "IF" into the mix ?

as in :

a=0

dict= {10: ['a',1,'c'], 20: ['d',2,'f']}

for i in dict:

  p+= 10 if dict[i][1] in [1,2,3,4,5] else 0


can i "squish" "for" and "if" together ? or will it be too perl-ish ?

    sum(10 for v in dict.values() if v[1] in [1,2,3,4,5])

or:

    sum((10 if v[1] in [1,2,3,4,5] else 0) for v in dict.values())

(in case you actually need something other than 0.)

Also, note that if your list of five values is actually much longer than five, then you want a set:

    sum((10 if v[1] in {1,2,3,4,5} else 0) for v in dict.values())

--Ned.





On Sun, Oct 8, 2017 at 12:37 AM, Andrew Z <form...@gmail.com> wrote:

Nathan, Bob - on the money. Thank you !

On Sat, Oct 7, 2017 at 11:30 PM, bob gailer <bgai...@gmail.com> wrote:

On 10/7/2017 11:17 PM, Nathan Hilterbrand wrote:

dict= {10: ['a',1,'c'], 20: ['d',2,'f']}
p = sum([dict[i][1] for i in dict])

Something like that?

Ah, but that's 2 lines.

sum(val[1] for val in  {10: ['a',1,'c'], 20: ['d',2,'f']}.values())

On Sat, Oct 7, 2017 at 11:07 PM, Andrew Z <form...@gmail.com> wrote:
Hello,
   i wonder how  can i accomplish the following as a one liner:

dict= {10: ['a',1,'c'], 20: ['d',2,'f']}
p = 0
for i in dict:
          p += dict[i][1]


Thank you
--
https://mail.python.org/mailman/listinfo/python-list


--
Image and video hosting by TinyPic



--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to