Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make my list print its elements in a fashioned order?

I currently have a populated list full of nodes in a path from one node to another. I need to stylize the printing of the path as such:

node -> node -> node -> node ->
node -> node

I currently have this code to do so:

        if len(path) %4 == 0:    
            for i in range(0, len(path)):
                if i+1 % 4 == 0:
                    print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]+' -> '+print_queue[3]+' -> ')
                    print_queue = []
                else:
                    print_queue.append(path[i])
        if len(path) %4 == 1:
            path.append(' ')
            for i in range(0, len(path)):
                if path[i] == ' ':
                    break
                if i+1 % 4 == 0:
                    print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]+' -> '+print_queue[3]+' -> ')
                    print_queue = []
                else:
                    print_queue.append(path[i])
        if len(path) %4 == 2:
            path.append(' ')
            for i in range(0, len(path)):
                if path[i] == ' ':
                    break
                if i+1 % 4 == 0:
                    print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]+' -> '+print_queue[3]+' -> ')
                    print_queue = []
                else:
                    print_queue.append(path[i])
        if len(path) %4 == 3:
            path.append(' ')
            for i in range(0, len(path)):
                if path[i] == ' ':
                    break
                if i+1 % 4 == 0:
                    print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]+' -> '+print_queue[3]+' -> ')
                    print_queue = []
                else:
                    print_queue.append(path[i])
        if print_queue != []:
            if len(print_queue) == 1:
                print(print_queue[0])
            if len(print_queue) == 2:
                print(print_queue[0]+' -> '+print_queue[1])
            if len(print_queue) == 3:
                print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2])

But the path is not being printed. The path list is populated, but the program does not give any output.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What do I need to change in order for this path to print?

>Solution :

It looks like you want to print each element in the path with " -> " between them.

print(" -> ".join(path))

should print them that way.

If you want a " -> " on the end too, just add one on the end in the print statement

print(" -> ".join(path) + " -> ")

so a path of ["a", "b", "c"] will print a -> b -> c ->

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading