#πŸ”’ Filter on dict.values() during for loop?

8 messages Β· Page 1 of 1 (latest)

untold condor
#

Hi, I have a for loop that looks like this:
if any(contact.new_messages for contact in contacts.values()):
Now I want to only apply this any on certain contacts, not all of them.
Contacts have a field visible and only those accounts with visible = True should be taken into account.
I know I can do something like:
if any(contact.new_messages for contact in filter(external_function, contacts.values())):
with

def external_function(c):
  return c.visible

but can i do this inline?
Like:
if any(contact.new_messages for contact in filter(c -> c.visible, contacts.values())):

bold galeBOT
#

@untold condor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

late ruin
#
if any(contact.new_messages for contact in filter(lambda c: c.visible, contacts.values())):
astral marlin
#

Wouldn't a if in the comprehension also work?

if any(contact.new_messages for contact in contacts.values() if contact.visible):
atomic urchin
#
if any(contact.visible and contact.new_messages for contact in contacts.values()):

Hmm

bold galeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.