We will split a Python list into sublists base on the index position.
The source code below and it’s quite self explanatory.
import numpy as np
#Original list to split
reviews_list = ['sarah', '16 contributions', '0', 'Good stay!', 'Jul 2022', 'A good place and stay with friends and family.',
'Written 10 August 2022', 'End of review.', 'Izzul S', '2 contributions', '0', 'Great customer service', 'Feb 2021 • Couples',
"Mr Jo was a friendly man and accomodated to our every need attentively.", 'Written 18 January 2022','End of review.', 'Angeline S',
'1 contribution', '0', 'Great staff!', 'Jul 2021 • Family', 'Mr Jo made our stay comfortable and always brought a smile to our face! He was very helpful in providing directions, advice and assistance with our luggage.',
'Written 14 July 2021', 'End of review.', 'TheReviewer', 'Singapore, Singapore11 contributions', '0', 'Alright.', 'Dec 2020',
'To be honest, it was not the best trip. The staff and food was alright, but I expected way more. It was also very clean.',
'Written 25 December 2020', 'End of review.', 'Lindsay Li','2 contributions', '0', 'A friendly staff', 'Oct 2020 • Couples',
'Kudos for hiring and having a great staff like Jo. He was a nice lad who’s very friendly, kind and helpful when we needed help.',
'Written 20 October 2020','End of review', 'shypixie A', '27 contributions', '0', 'Happy place to be at', 'Mar 2020',
'A total different feel of place from the rest of Singapore. Standing alone, very entertaining place. Many options for you to choose what you really want to do!', 'Written 16 May 2020', 'End of review.',
'Paul B', 'Algaida, Spain778 contributions', '1', '" getting on in age "', 'Feb 2020',
'we were here 12 years ago , many of the new attractions are new and were not even envisaged 12 years ago, so it was a pleasant sight to behold how the island has changed in a decade , yes there are continual renovations going on , however we found the island to be looking old , dreary , tired , unkept , lacking in the WOW effect ,',
'the beaches were not over full on Saturday morning / afternoon , what we did not appreciate were some of the prices for rides , cable car etc , extremely expensive and we noticed that many cars were empty .',
'The island is a main attraction for those seeking a getaway from the city , prices should be reflective of those who chose to visit .', 'Read more', 'Written 1 March 2020', 'End of review.',
'Deepika025', 'Durban, South Africa73 contributions', '0', 'Great', 'Dec 2019', "I really enjoyed this place. Has lots of stuff you can do. As usual everything is expensive. The show they have at night is just too AMAZING. I went for Wings of Time and it's something spectacular to see. You have to visit this place",
'Written 27 January 2020', 'End of review.']
#print(reviews_list)
#Get the indices base on standard key phrase
delimiter_text = 'End of review'
index_list = []
index = -1
#Get the indices of the text that you want to split at.
#It has to be a common occurance for all the sub-lists
for item in reviews_list:
index += 1
if (delimiter_text in item ) :
index_list.append(index)
#remove last element(last index).
#Since the split will occur at the index ie happen before 'End of review'
# hence we +1 to make the split after 'End of review'
index_list.pop()
index_list = [item +1 for item in index_list]
print (index_list)
#Split List into sublists
reviews_sublist = [sublist.tolist() for sublist in np.split(reviews_list, index_list)]
print(reviews_sublist)
#Print sublist as independent lists
for sublist in reviews_sublist:
print(sublist)
print('\n')
The output :
[8, 16, 24, 32, 40, 48, 59]
[[‘sarah’, ’16 contributions’, ‘0’, ‘Good stay!’, ‘Jul 2022’, ‘A good place and stay with friends and family.’, ‘Written 10 August 2022’, ‘End of review.’], [‘Izzul S’, ‘2 contributions’, ‘0’, ‘Great customer service’, ‘Feb 2021 • Couples’, ‘Mr Jo was a friendly man and accomodated to our every need attentively.’, ‘Written 18 January 2022’, ‘End of review.’], [‘Angeline S’, ‘1 contribution’, ‘0’, ‘Great staff!’, ‘Jul 2021 • Family’, ‘Mr Jo made our stay comfortable and always brought a smile to our face! He was very helpful in providing directions, advice and assistance with our luggage.’, ‘Written 14 July 2021’, ‘End of review.’], [‘TheReviewer’, ‘Singapore, Singapore11 contributions’, ‘0’, ‘Alright.’, ‘Dec 2020’, ‘To be honest, it was not the best trip. The staff and food was alright, but I expected way more. It was also very clean.’, ‘Written 25 December 2020’, ‘End of review.’], [‘Lindsay Li’, ‘2 contributions’, ‘0’, ‘A friendly staff’, ‘Oct 2020 • Couples’, ‘Kudos for hiring and having a great staff like Jo. He was a
nice lad who’s very friendly, kind and helpful when we needed help.’, ‘Written 20 October 2020’, ‘End of review’], [‘shypixie A’, ’27 contributions’, ‘0’, ‘Happy place to be at’, ‘Mar 2020’, ‘A total different feel of place from the rest of Singapore. Standing alone, very entertaining place. Many options for you to choose what you really want to do!’, ‘Written 16 May 2020’, ‘End of review.’], [‘Paul B’, ‘Algaida, Spain778 contributions’, ‘1’, ‘” getting on in age “‘, ‘Feb 2020’, ‘we were here 12 years ago , many of the new attractions are new and were not even envisaged 12 years ago, so it was a pleasant sight to behold how the island has changed in a decade , yes there are continual renovations going on , however we found the island to be looking old , dreary , tired , unkept , lacking in the WOW effect ,’, ‘the beaches were not over full on Saturday morning / afternoon , what we did not appreciate were some of the prices for rides , cable car etc , extremely expensive and we noticed that many cars were empty .’, ‘The island is a main attraction for those seeking a getaway from the city , prices should be reflective of those who chose to visit .’, ‘Read more’, ‘Written 1 March 2020’, ‘End of review.’], [‘Deepika025’, ‘Durban, South Africa73 contributions’, ‘0’, ‘Great’, ‘Dec 2019’, “I really enjoyed this place. Has lots of stuff you can do. As usual everything
is expensive. The show they have at night is just too AMAZING. I went for Wings of Time and it’s something spectacular to see. You have to visit this place”, ‘Written 27 January 2020’, ‘End of review.’]]
[‘sarah’, ’16 contributions’, ‘0’, ‘Good stay!’, ‘Jul 2022’, ‘A good place and stay with friends and family.’, ‘Written 10 August 2022’, ‘End of review.’]
[‘Izzul S’, ‘2 contributions’, ‘0’, ‘Great customer service’, ‘Feb 2021 • Couples’, ‘Mr Jo was a friendly man and accomodated to our every need attentively.’, ‘Written 18 January 2022’, ‘End of review.’]
[‘Angeline S’, ‘1 contribution’, ‘0’, ‘Great staff!’, ‘Jul 2021 • Family’, ‘Mr Jo made our stay comfortable and always brought a smile to our face! He was very helpful in providing directions, advice and assistance with our luggage.’, ‘Written 14 July 2021’, ‘End of review.’]
[‘TheReviewer’, ‘Singapore, Singapore11 contributions’, ‘0’, ‘Alright.’, ‘Dec 2020’, ‘To be honest, it was not the best trip. The staff and food was alright, but I expected
way more. It was also very clean.’, ‘Written 25 December 2020’, ‘End of review.’]
[‘Lindsay Li’, ‘2 contributions’, ‘0’, ‘A friendly staff’, ‘Oct 2020 • Couples’, ‘Kudos for hiring and having a great staff like Jo. He was a nice lad who’s very friendly, kind and helpful when we needed help.’, ‘Written 20 October 2020’, ‘End of review’]
[‘shypixie A’, ’27 contributions’, ‘0’, ‘Happy place to be at’, ‘Mar 2020’, ‘A total different feel of place from the rest of Singapore. Standing alone, very entertaining place. Many options for you to choose what you really want to do!’, ‘Written 16 May 2020’, ‘End of review.’]
[‘Paul B’, ‘Algaida, Spain778 contributions’, ‘1’, ‘” getting on in age “‘, ‘Feb 2020’, ‘we were here 12 years ago , many of the new attractions are new and were not even envisaged 12 years ago, so it was a pleasant sight to behold how the island has changed in a decade , yes there are continual renovations going on , however we found the island to be looking old , dreary , tired , unkept , lacking in the WOW effect ,’, ‘the beaches were not over full on Saturday morning / afternoon , what we did not appreciate were some of the prices for rides , cable car etc , extremely expensive and we noticed that many cars were empty .’, ‘The island is a main attraction for those seeking a getaway from the city , prices should be reflective of those who chose to visit .’, ‘Read more’, ‘Written 1 March 2020’, ‘End of review.’]
[‘Deepika025’, ‘Durban, South Africa73 contributions’, ‘0’, ‘Great’, ‘Dec 2019’, “I really enjoyed this place. Has lots of stuff you can do. As usual everything is expensive. The show they have at night is just too AMAZING. I went for Wings of Time and it’s something spectacular to see. You have to visit this place”, ‘Written 27 January 2020’,
‘End of review.’]