0

I have a list:

mylist = [
"hello", "my", "name", "is", "frank", "and", "it", "is", "great", "to", "be", "here", "in", "this", "great", "country"

]

and am using

for i in range(0,len(mylist)):
    print(mylist[i])

to print out each item and it works as it prints out every word in a new line.

What I would like to do is to be able to treat multiple words in the list as one by changing the value of a variable k:

i.e I would like to instead of it outputting

hello                                                                                                                   
my                                                                                                                      
name                                                                                                                    
is                                                                                                                      
frank                                                                                                                   
and                                                                                                                     
it                                                                                                                      
is                                                                                                                      
great                                                                                                                   
to                                                                                                                      
be                                                                                                                      
here                                                                                                                    
in                                                                                                                      
this                                                                                                                    
great                                                                                                                   
country 

I would like to if k = 2 to output

hello my                                                                                                                      
name is                                                                                                                      
frank and                                                                                                                     
it is                                                                                                                      
great to                                                                                                                      
be here                                                                                                                    
in this                                                                                                                    
great country 

should there be an odd number of items in the list, the last word should be alone in a line

if k = 3 then there should be 3 words in a line

Is there any way to do this

Thank you in advance

1 Answers1

2

You can use this chunking recipe:

def chunks(L, n):
    """Yield successive n-sized chunks from L."""
    for i in range(0, len(L), n):
        yield L[i:i + n]

Then iterate with str.join or use sequence unpacking with the sep argument of print:

for i in chunks(mylist, 2):
    print(' '.join(i))
    # alternatively, unpack and use sep argument:
    # print(*i, sep=' ')

# hello my
# name is
# frank and
# it is
# great to
# be here
# in this
# great country
jpp
  • 159,742
  • 34
  • 281
  • 339