[Tutor] interesting str behavior ...

David Broadwell dbroadwell@mindspring.com
Thu May 15 23:46:01 2003


This will be trivial for most but;

I have known strings are iterable for awhile ...
However, the code below struck it home.
Now I believe they are.

>>> from string import letters
>>> temp = []
>>> temp.extend(letters)
>>> temp
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

Ah learning from mistakes ... I was testing something in the interpreter,
and grabbed a string not a list to look at list.extend().

The closest I can code to 'extend' is;
>>> temp = []
>>> temp.append([item for item in letters])
>>> temp
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z']]

Though that isn't perfect ... it is much the same semantics.

( Just sharing where I stub my toes ... )

--

David Broadwell