Python Forum
How to invoke a function with return statement in list comprehension?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to invoke a function with return statement in list comprehension?
#1
Hi All,

I want to invoke a function with return statement using list comprehension or even lambda (if possible) for the better performance. Below is code snippet where I am trying to achieve that in list comprehension where if that function return Fail then break the flow and control move to next statement.
def func(var):
    return True if var % 2 else False

l = [2, 2, 3, 4]
new_list = [out for var in l out = func(var) if out == False break]
print(new_list)
Output:
[True, True]
But then I could not able to achieve the desired result, I would really appreciate if any suggestion on this.

Similarly how to assign a value using list comprehension?

Regards,
Maiya
Reply


Messages In This Thread
How to invoke a function with return statement in list comprehension? - by maiya - Jul-17-2021, 09:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 542 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,715 Jan-05-2024, 08:30 PM
Last Post: sgrey
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 484 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  nested function return MHGhonaim 2 619 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,299 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Using list comprehension with 'yield' in function tester_V 5 1,261 Apr-02-2023, 06:31 PM
Last Post: tester_V
  function return boolean based on GPIO pin reading caslor 2 1,192 Feb-04-2023, 12:30 PM
Last Post: caslor
  Need help with Return statement Columbo 13 2,291 Sep-17-2022, 04:03 PM
Last Post: Columbo
  list comprehension 3lnyn0 4 1,426 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,675 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020