I performed this operation to retrieve a queryset:
Name.objects.values_list('name', flat=True)
And it returns these results:
[u'accelerate', u'acute', u'bear', u'big']
The results are all in unicode (u'). How do I remove them all so that I get the result:
['accelerate', 'acute', 'bear', 'big']
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)]
You could call str
on all the values (note that map is a bit lazy, list()
added to immediately turn it back into an indexable object):
thingy = list(map(str, [u'accelerate', u'acute', u'bear', u'big']))
Or use a list comprehension:
[str(item) for item in [u'accelerate', u'acute', u'bear', u'big']]
In the end though, why would you require them to be str
explicitly; added to a django template (like {{ value }}
), the u's will disappear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With