Re: for/ if against dict - one liner
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@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 > -- https://mail.python.org/mailman/listinfo/python-list
Re: for/ if against dict - one liner
On 2017-11-14 06:44, 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: Don't you mean dct.items() >if k >= 2: > # do magnificent things > > Thank you > AZ > -- Thomas Jollans -- https://mail.python.org/mailman/listinfo/python-list
Re: for/ if against dict - one liner
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
Re: for/ if against dict - one liner
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: for/ if against dict - one liner
On 2017-11-14 07:29, Stefan Ram wrote: > Andrew Zwrites: >> i wonder how do i get the "for" and "if" to work against a dictionary in >> one line? > > dict ={ 1 : "one", 2 : "two", 3 : "three" } > print( *( ( str( key )+ ' ' + str( dict[ key ])) for key in dict if key >= 2 > ), sep='\n' ) > > prints: > > 2 two > 3 three > We can build something nicer than that, surely. The repeated str() calls and dictionary lookups just look like noise to my eyes. print('\n'.join(f'{k} {v}' for k, v in dict.items() if k >= 2)) But indeed, you can build concise dictionary filters like that with generator expressions and list comprehensions. -- Thomas Jollans -- https://mail.python.org/mailman/listinfo/python-list
for/ if against dict - one liner
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 -- https://mail.python.org/mailman/listinfo/python-list