Python giving UnicodeWarning when calling remove() on a list containing unicode strings
I have two lists containing unicode strings. I'm trying to remove every element from availablesongs that also appears in recentsongs.
The following code is causing the problem (exception clause commented out for debugging):
for x in recentsongs: #try: availablesongs.remove(x) #except ValueError: # pass
This code works fine when the lists contain ASCII-only strings, but when characters from other languages are introduced, it fails:
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal availablesongs.remove(x) ValueError: list.remove(x): x not in list
The error is coming from the remove() function itself, which has me stumped. How can I work around this?
What I would do:
os.listdir() was causing some of my problems because it sometimes returns unicode strings and sometimes doesn't. The function to_unicode_or_bust works to fix the rest of it.