Exercise 86

Given a list L, write a python algorithm which returns the list of integer elements of L. Example: if

 L = ["Python3", 91, "Java2", 95]

, the algorithm returns the list

[91, 95]

Solution

def listInteger (L):
# initialization of the list of integers
lInteger = []
for x in L:
# we test if the element x is an integer
if type (x) == int:
lInteger.append (x)
return lInteger

# Example
L = ["Python3", 91, "Java2", 95]
print (listInteger (L))
# The output is: [91, 95]
Younes Derfoufi
my-courses.net

Leave a Reply