Re: finding a component in a list of pairs

2019-06-24 Thread Peter Pearson
[snip]
> print( dict( pairs ).get( 'sun', '(unknown)' ))

You probably know this, but . . . just in case . . .

If you're doing this many times, you'll want to construct the dict
just once and then make many references to the dict, rather than
re-constructing the dict every time you want to look up an element.

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: finding a component in a list of pairs

2019-06-23 Thread Peter Pearson
On 22 Jun 2019 13:24:38 GMT, Stefan Ram  wrote:
[snip]
>
> print( next( ( pair for pair in pairs if pair[ 0 ]== 'sun' ), 
>  ( 0, '(unbekannt)' ))[ 1 ])
> print( next( itertools.dropwhile( lambda pair: pair[ 0 ]!= 'sun', pairs ))
>  [ 1 ])
[snip]
>
>   The last two lines of the program show two different
>   approaches to search for the translation of »sun«.
>
>   Which approach is better? Or, do you have yet a better idea
>   about how to find the translation of »sun« in »pairs«?

Are you allowed to use a dict?

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list