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.

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 ->

If you want it to break on multiples of 4 nodes you can break the array into chunks of 4 first

path = ["a", "b", "c", "d", "e"]
print(" -> n".join([" -> ".join(path[i:i+4]) for i in range(0, len(path), 4)]))

which will print

a -> b -> c -> d -> 
e


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x