mapping a statement over all list items

Terry Reedy tjreedy at home.com
Sun Nov 11 10:32:24 EST 2001


"Kevin Christie" <crispiewm at hotmail.com> wrote in message
news:3BEE4077.8070407 at hotmail.com...
> Hello all!
>
>    I have list Foo, of strings. Each string has a special character
'-'
> within the string. What I want is for Python to split each string
list
> item by '-' and assign the resulting lists to the original list
item.
> Basically, I want to apply:

If you are satisfied with a *new* list with corresponding split-up
items,
then the map or comprehension idioms work great.  If you literally
mean
'assign the resulting lists to the original list item',
then you need a for loop with explicit indexes.

>>> import string
>>> sl = ['abc-xyz', 'black-blue', '123-987']
>>> for i in range(len(sl)): sl[i] = string.split(sl[i], '-')
...
>>> sl
[['abc', 'xyz'], ['black', 'blue'], ['123', '987']]






More information about the Python-list mailing list