Exercise 89 *

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

Solution

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

# browse the elements of list L
for word in L:
word = str (word)
# browse the elements of the word string and search for the entire elements
for x in word:
if x.isdigit() and int(x) not in list_digit:
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