Skip to content

Instantly share code, notes, and snippets.

@niloy-barua
Created March 23, 2017 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niloy-barua/7c4b5c71914fb878dcc5e5763d232b56 to your computer and use it in GitHub Desktop.
Save niloy-barua/7c4b5c71914fb878dcc5e5763d232b56 to your computer and use it in GitHub Desktop.
def toEvenOdd(numList):
"""
list: list containing number
return: two lists. first containing even numbers, second containing odd
numbers
"""
evenList=[] #will contain the even numbers after next iterative part
oddList=[] #will contain the odd numbers after next iterative part
for i in numList:
if i%2==0:
evenList.append(i)
else:
oddList.append(i)
return evenList, oddList
@uroybd
Copy link

uroybd commented Mar 23, 2017

def to_even_odd(num_list):
    odd, even = [], []
    [even.append(x) if x%2==0 else odd.append(x) for x in num_list]
    return even, odd

@uroybd
Copy link

uroybd commented Mar 23, 2017

এইটায় আমরা একটা is_even function ব্যবহার করছি ফিল্টার করতে, ফাংশনটা রিইউজেবল, এইটুকুই সুবিধা:

def is_even(num):
    return True if num % 2 == 0 else False

def to_even_odd_filter(num_list):
    even = [i for i in num_list if is_even(i)]
    odd = [i for i in num_list if not is_even(i)]
    return even, odd

আমরা is_even কে ফিল্টারে পাস করতে পারি। যেমন শুধু ইভেন নাম্বারগুলো পাওয়া যাবে এইভাবে:

num_list = [1, 2, 3, 4]
list(filter(is_even, num_list))
# output [2, 4]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment