Jorge Padilla
Jorge Padilla

Reputation: 67

How to break line within a list?

Here's my code:

user_input = input("Enter a saying or poem: ")

words_list = user_input.split()

words_list_length = len(words_list)


def word_mixer(words_list):

    words_list.sort()

    final_list = []

    if(len(words_list) <= 5):

        final_list = words_list 


    else:

        while(len(words_list) > 5):

            first_word_popped = words_list.pop(-5)

            final_list.append(first_word_popped)

            second_word_popped = words_list.pop(0)

            final_list.append(second_word_popped)

            third_word_popped = words_list.pop(-1)

            final_list.append(third_word_popped) 

    return final_list

So what I want is to break to a new line at the end of the while loop, so that when the code exits the while loop, the elements in the final_list variable are printed (or accomodated) 3 by 3 since it appends 3 elements in each iteration. I've seen people use the join() method but I'm not sure how to implement it here in case that's how I should solve the problem.

I've updated the code so you guys could get more context. An example input would be: "Hello there, this is a string input given by the user". An expected output would be :

string given user
Hello this by

As you can see, the string was sorted and it should print (if I added a print statement to my code, of course) 3 words in each row. But I can't find a way to do it.

This exercise is from a Python course I'm taking in the edx.org page. There, they give this input/output example:

enter image description here

Just ignore the words being turned to upper/lower case. But as you can see, the list is printed 3 by 3 by adding a "\n" to the list. How do I do that?

Upvotes: 0

Views: 1160

Answers (2)

Viral Modi
Viral Modi

Reputation: 2135

If you just want to print, you can print it inside the loop itself with a "\n" at the end of the loop. But if you want to store it in a format where you can use each group of words separately, then you could do something like:

words_list = ["Hello", "there", "this", "is", "a", "string", "input", "given", "by", "the", "user"]
final_list_2 = list()
while(len(words_list) > 5):
    final_list = list()
    first_word_popped = words_list.pop(-5)
    final_list.append(first_word_popped)
    second_word_popped = words_list.pop(0)
    final_list.append(second_word_popped)
    third_word_popped = words_list.pop(-1)
    final_list.append(third_word_popped) 
    final_list_2.append(final_list)

for final_list in final_list_2:
    print (" ".join(final_list))

OUTPUT:

input Hello user a there the

Upvotes: 1

VISWESWARAN NAGASIVAM
VISWESWARAN NAGASIVAM

Reputation: 352

So you need to append a new line for every three words, this is simple...

LIST = ["or", "brushed", "thy", "not", "little", "though", "me?", "summers?", "thee?"]
# Insert the new line
for i in range(len(LIST)):
    if i%3==0 and i>0:
        LIST.insert(i, "\n")
# Join the sentence
para = " ".join(LIST)
# Print the result
for i in para.split("\n"):
    print(i.strip())

you will get.

or brushed thy
not little
though me? summers? thee?

Upvotes: 0

Related Questions