Home > database >  Join the strings from list for 4 set each
Join the strings from list for 4 set each

Time:07-11

i have a string list , i want to join the strings (four occurrence each) in index 0, 1 , 2 respectively in a new list

Old_List=

[ 'User','need','to','log','in','Username','need','to','enter','in','Password','need','to','enter','here']

i want my list to look like this

Newlist=

   ['User need to log in', 'Username need to Enter in ', 'Password need to enter here']

CodePudding user response:

It could be done by splitting a list into chunks by using list-comprehension and joining them back with join method.

Old_List = ['User', 'need', 'to', 'log', 'in', 'Username', 'need',
            'to', 'enter', 'in', 'Password', 'need', 'to', 'enter', 'here']
Chunk_Size = 5
Newlist = [Old_List[sentenceStart:sentenceStart Chunk_Size]
           for sentenceStart in range(0, len(Old_List), Chunk_Size)]

print([' '.join(sentence) for sentence in Newlist])
#    ['User need to log in', 'Username need to enter in', 'Password need to enter here']
  • Related