Exercise 88 *

Given a list L, write a python algorithm which returns the list of digits contained within the elements of the list L. Example if L = ["Python3", 91, "Java2", 95], the algorithm returns the list [3, 9, 1, 2, 9, 5]

Solution

L = ["Python3", 91, "Java2", 95]
# initialization of the list of digits
list_digit = []

# browsing through all elements of the list L
for word in L:
word = str(word)
# browsing through all elements of the 'word' string and search for the entire elements
for x in word:
if x.isdigit ():
x = int (x)
list_digit.append (x)

print (list_digit)
# The output is: [3, 9, 1, 2, 9, 5]
Younes Derfoufi
my-courses.net

Leave a Reply