1

Jupyter Notebook Snippet

can someone please tell me why is my list not empty?

#Function that verify if a word is a latin word (delivers true) or not (delivers false)
# -*- coding: utf-8 -*-

def is_latin(word):
try:
    word.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError :
    print(word) # for debugging purposes
    return False
else:
    return True

# Function to eliminate a latin word from a given List of Strings

def eliminate_non_latin(initial_list):
for element in initial_list:
    if not is_latin(element):
        initial_list.remove(element)
return initial_list

#Testing the two functions

Test=['幸福','المغرب']
print(eliminate_non_latin(Test))

Output:

  • 幸福 ====> is_latin function works fine (Debugging)
  • ['المغرب'] ====> here's my problem this list should be empty!

Am i missing something?

  • 1
    yes the first output was just for debugging purposes. It triggers the except clause but the second output is the resulted list. It should be empty since the two words are not latin words. – Lahmidi Mustapha Mar 28 '21 at 14:53

0 Answers0