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
#2
from itertools import takewhile


def func(number):
    return True if number % 2 else False


numbers = [2, 2, 3, 4]
new_list = [number for number in numbers if not func(number)]
print(new_list)

new_list2 = list(takewhile(lambda number: not func(number), numbers))
print(new_list2)
Output:
[2, 2, 4] [2, 2]
Reply
#3
I want to store the return value from the function not the list numbers.
Reply
#4
from itertools import takewhile


def func(number):
    return True if number % 2 else False


numbers = [2, 2, 3, 4]
new_list = [func(number) for number in numbers if not func(number)]
print(new_list)

new_list2 = [func(number) for number in takewhile(
    lambda number: not func(number), numbers)]
print(new_list2)
Output:
[False, False, False] [False, False]
maiya likes this post
Reply
#5
(Jul-17-2021, 12:01 PM)Yoriz Wrote:
from itertools import takewhile


def func(number):
    return True if number % 2 else False


numbers = [2, 2, 3, 4]
new_list = [func(number) for number in numbers if not func(number)]
print(new_list)

new_list2 = [func(number) for number in takewhile(
    lambda number: not func(number), numbers)]
print(new_list2)
Output:
[False, False, False] [False, False]
Could you please explain the code sir? and here is that really required to call func method twice?
Regards,
Maiya
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 299 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need to parse a list of boolean columns inside a list and return true values Python84 4 850 Jan-09-2022, 02:39 AM
Last Post: Python84
  return vs. print in nested function example Mark17 4 535 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 574 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  List comprehension used differently coder_sw99 3 850 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  Change a list to integer so I can use IF statement buckssg 3 1,118 Sep-21-2021, 02:58 AM
Last Post: bowlofred
  An IF statement with a List variable dedesssse 3 1,156 Jul-08-2021, 05:58 PM
Last Post: perfringo
  List comprehension and Lambda cametan 2 1,257 Jun-08-2021, 08:29 AM
Last Post: cametan
  Function - Return multiple values tester_V 10 2,467 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Get return value from a threaded function Reverend_Jim 3 8,461 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim

Forum Jump:

User Panel Messages

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