Exercise 66
Write a Python algorithm to extract the list of integers from a list of numbers. Example: if
L = [2.5 , 11.54 , 3 , 7.35 , 5 , 6.5 , 9]
, the algorithm returns the list
[3, 5, 9]
Solution
def extractInt (L):
# initialization of the integer list
listInt = []
# iterate over the elements of L and extract the integers
for x in L:
if type (x) == int:
listInt.append (x)
return listInt
# Example
L = [2.5, 11.54, 3, 7.35, 5, 6.5, 9]
print (extractInt (L)) # the output is: [3, 5, 9]
Younes Derfoufi
my-courses.net
my-courses.net