tags:

views:

36

answers:

2

Hi,

I have a list and a value and want to check if the value is not in the list.

list = [u'first record', u'second record']
value = 'first record'

if value not in list:
    do something

however this is not working and I think it has something to do with the list values having a u at the start, how can I fix this? And before someone suggests the list is returned from Django queryset so I can't just take the u out of the code :)

Thanks

+1  A: 

I can't see why it would not work:

s1 = 'hello'
s2 = u'hello'
s1 == s2 # True
Olivier
yeah you are right. I think it must be something to do with the way django is returning the objects rather than the python logic
John
+2  A: 

unicode(value) transforms your 'first record' into u'first record'. That might fix your issues. However, depending on the contents this might fail and you'll have to use the .encode('charset') function strings have.

PS: Your example is bad as those strings are equal in unicode and non-unicode and thus your example works fine.

ThiefMaster
yeah I agree. The code I am using is being returned from Django models but I didn't want to include all this extra stuff so tried to shorten to just python code it but like you say in my example it would work.
John
adding the unicode(value) worked. Thanks.
John