Encode Python list to UTF-8

Tom picture Tom · Jun 6, 2013 · Viewed 75.2k times · Source

I have a python list that looks like that:

list = [u'a', u'b', u'c']

Now I want to encode it in UTF-8. Therefore I though I should use:

list = list[0].encode("utf-8")

But print list gives only

a

meaning the first element of the list. Not even a list anymore. What am I doing wrong?

Answer

jamylak picture jamylak · Jun 6, 2013
>>> items =  [u'a', u'b', u'c']
>>> [x.encode('utf-8') for x in items]
['a', 'b', 'c']