I have a list of elements that occur in order and I'd like to add one at a time to a list.
Desired output:
'first_page'
'first_page + second_page'
'first_page + second_page + third_page'
'first_page + second_page + third_page + fourth_page'
...
Data structure:
page_list = ['first_page', 'second_page', 'third_page', 'fourth_page']
How can I get desired output? Thanks!
So far, I'm able to add the next page to the previous page using this function:
new_list = []
for index, elem in enumerate(page_list):
if(index<(len(page_list)-1)):
new_list.append(elem + ' + ' + page_list[index+1])
else:
new_list.append(elem)
which returns:
['first_page + second_page',
'second_page + third_page',
'third_page + fourth_page',
'fourth_page']
Use
join()to create a delimited string.You can use incrementing slices of the original list as the list to slice.
Or you can keep appending to another list before joining.