On Sat, May 7, 2022 at 9:15 AM Jonathan Fine <[email protected]> wrote:
> Here's my preferred solution, using Python builtins:
> >>> users = [
> ... {'id': 1,'name': 'john'},
> ... {'id': 2, 'name': 'anna'},
> ... {'id': 3, 'name': 'bruce'},
> ... ]
> >>> func = (lambda user: user['id'] == 2)
>
> >>> next(filter(func, users))
> {'id': 2, 'name': 'anna'}
>
The OP did suggest a comprehension version, which I personally prefer --
but thought it " feels a bit janky" -- I can say that lambda/filter feels
even jankier to me :-)
you could create a new class for holding a collection (or set) of members.
>
and then you'd be writing this code in your class -- but that's a good
thing, as you could then refactor it to optimize it or use persistent
storage, or ....
Anyway, I agree that once. you need to customize your matching function,
you might as just write the code.
-CHB
--
Christopher Barker, PhD (Chris)
Python Language Consulting
- Teaching
- Scientific Software Development
- Desktop GUI and Web Development
- wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/TL2H32CBLMQNQVLQCZCANOXXIVTPYX2P/
Code of Conduct: http://python.org/psf/codeofconduct/