jdtoh jdtoh - 10 months ago 142
Python Question

Django remove unicode in values_list

I did this operation:

definitions.objects.values_list('title', flat=True)

And it returns these results:

[u'accelerate', u'acute', u'bear', u'big'...]

You will realize that the results are all in unicode (u'). How do I remove them all so that I get the result:

['accelerate', 'acute', 'bear', 'big' ...]

Thanks in advance!

Answer Source

If you want to encode in utf8, you can simply do:

definitions_list = [definition.encode("utf8") for definition in definitions.objects.values_list('title', flat=True)]