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

Securing front-end JS app and Rails API communication with SSL/TLS

ruby-on-rails security rest ssl backbone.js

I have a Rails JSON API with token based authentication, and a static front-end client using Backbone.js (which is served from a different Rails server).

Call a controller function recursive from twig template

symfony2 twig symfony-2.3

I have this function in a CategoryBundle:CategoryTreeBuilderController:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.