0

Can any one tell me how to split a list, if its possible. Want to split it word by word.

my list contains links like:

 ['14th_century;15th_century;16th_century;Pacific_Ocean;Atlantic_Ocean;Accra;Africa;Atlantic_slave_trade;African_slave_trade']

Now, i want to use the split method, to split up 14th_century and 15th_century, so it is 2 words, and so on with all links.

So for every sign " ; " it should just split it.

right now i made a for loop.

for line in loops:

UPDATE:

have done it so far as this.

links = []    
for line in newPath:
    links.append(line[3:4])

old_list = []
new_list = []

old_list = links
new_list = old_list[0].split(';')

print new_list
1
  • What is your definition of word (stream of characters separated by underscrores ?) Commented Apr 7, 2014 at 9:12

2 Answers 2

1

You can simply do:

my_list = old_list[0].split(';')

Examples

>>> old_list = ['14th_century;15th_century;16th_century;Pacific_Ocean;Atlantic_Ocean;Accra;Africa;Atlantic_slave_trade;African_slave_trade']

>>> my_list = old_list[0].split(';')
['14th_century', '15th_century', '16th_century', 'Pacific_Ocean', 'Atlantic_Ocean', 'Accra', 'Africa', 'Atlantic_slave_trade', 'African_slave_trade']
Sign up to request clarification or add additional context in comments.

8 Comments

Then i get an error 'list' object has no attribute 'split'
@pixel, Are you sure you did old_list[0] and not just old_list since your string is in a list in your question
Have added new code in top
@Pixel, Note, your old_list should be a list of length 1 which contains one long string as shown by the first code in your question.
@pixel, please my example in my answer
|
0

You can simply do:

paths = ['abc;def;ghi', 'jkl;mno;pqr']
paths = [path.split(';') for path in paths]
>>> paths
[['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']]

2 Comments

also give me same error, 'list' object has no attribute 'split'
Given that paths is a list of strings. Check my answer again for an example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.