Python-courses-python-exercises

Exercise 26

For a given list L of integers, write an algorithm in Python that return the tuple of lists (l_even, l_odd) where l_even designates the list of even integers of L and l_odd designates the list of odd integers of L. Example for L = [11, 3, 22, 14, 31, 18, 12, 7]the program returns the pair of lists: ([22, 14, 18, 12], [11, 3, 31, 7])

Solution

def oddEven (L):
# initialization of even and odd integer lists
l_even = []
l_odd = []
for n in L:
if n% 2 == 0:
l_even.append (n)
else:
l_odd.append (n)
return (l_even, l_odd)

#Example
L = [11, 3, 22, 14, 31, 18, 12, 7]
print (oddEven (L)) # the output is: ([22, 14, 18, 12], [11, 3, 31, 7])
Younes Derfoufi
my-courses.net

Leave a Reply