0

I want to remove 'u' from every element in the list, can anybody help me?

 [u'four', u'gag', u'prefix', u'woods'] 
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
HIREN GALA
  • 109
  • 3
  • 10

2 Answers2

1

The issue is with the encoding of strings. Do this :

l = [u'four', u'gag', u'prefix', u'woods']
l2 = [i.encode('UTF-8') for i in l]

print l2
['four', 'gag', 'prefix', 'woods']
Gaurav Dhama
  • 1,346
  • 8
  • 19
1

The u is an attribute that tells what type of string it is. If it was a byte string, this would be b. If you call type on these, they will return String. The difference between Unicode and something like ASCII is that Unicode is a super-set of ASCII that is the same for 0-127, but has more capability to represent different types of characters. These can be UTF-8 or UTF-32 or whatever, but generally are larger than one byte.

It should behave the same for 99% of the things that you want to do, but you can also change the encoding if you have a function that needs a very particular type of string.