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?

Answers


What I would do:

list(set(availablesongs)-set(recentsongs))

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.


Need Your Help

Step to build a built in kernel module?

linux module kernel driver device

I can build a loadable module and it is working with the application successfully. Now I'm trying to include this driver in kernel driver folder as a built-in driver. But when i tried this, there i...

How can I drop a table if it exists in SQL Server 2000?

sql sql-server

I have a DTS package that drops a table then creates it and populates it but sometimes something happens and the package fails after the drop table. If it's rerun it fails cuz the table hasn't been