mapping a statement over all list items
tjreedy at home.com
Sun Nov 11 16:32:24 CET 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
> item by '-' and assign the resulting lists to the original list
> Basically, I want to apply:
If you are satisfied with a *new* list with corresponding split-up
then the map or comprehension idioms work great. If you literally
'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], '-')
[['abc', 'xyz'], ['black', 'blue'], ['123', '987']]
More information about the Python-list