[Tutor] Unicode in List Object

Tim Golden mail at timgolden.me.uk
Mon Mar 26 18:02:38 CEST 2007


Roman Kreuzhuber wrote:
> Thanks for the quick response!
> I see! Oh I didn't realize that it's not the list which raises an error.
> For a test I tried to insert a string containing a unicode character as 
> follows:
> 
> ListObject = []
> ListObject.insert(0,u"Möälasdji")

By the way, aside from the Unicode-encoding issue, you seem
to be coming from a language background where you have to
declare things. In python, you could do the above as:

l = [u"Möälasdji"]

or, if you have to add several things to the list,
one at a time, say from a text file, you can use
append for the most obvious add-to-end case:

l = []
l.append ("Hello")
l.append ("World")

Obviously, if you do really want to add at the
beginning of the list, you can use insert (0, ...)
as you have, but it might just as good to append
as above, and then to reverse the list, ie:

l = []
l.insert (0, "Hello")
l.insert (0, "World")

is equivalent to:

l = []
l.append ("Hello")
l.append ("World")
l.reverse ()

TJG


More information about the Tutor mailing list