Python Forum
number of items per line - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: number of items per line (/thread-6372.html)



number of items per line - kiki1113 - Nov-19-2017

Quote: I've created a list of numbers. I need to print 10 numbers per line, so they're all lined up one underneath the other. I'm having trouble trying to figure out how to remove the brackets [] and know how to print them lined up. Any advice welcome. Thanks!

def odd() :
    new_list = []
    for i in range(1,50,2) :
        if i % 2 != 0 :
            new_list.append(i)    
    for i in range(0, len(new_list), 10):
        print(new_list[i:i + 10])
    
odd()
Quote:my output
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
[21, 23, 25, 27, 29, 31, 33, 35, 37, 39]
[41, 43, 45, 47, 49]
:


Quote:EDITED: I've just realized I've gone about this all wrong. My final output should not include commas or brackets. I honestly have no idea how to fix this. Wall

Instead I should have:
1 3 5 7 9 11 13 15 17 19
21 23 25 27 29 31 33 35 37 39

With the values aligning on the right (can't get it to display properly here).


RE: number of items per line - wavic - Nov-19-2017

You have to deal with the string formatting. print("{:2}".format(list_item) will use two positions to print a number for example and if the number is one digit it will be aligned to the right.


RE: number of items per line - kiki1113 - Nov-19-2017

Thanks Wavic. Unfortuantely that didn't work for me. The issue I'm having right now is how to print the list without the brackets or commas and THEN right aligning them. Any thoughts?


RE: number of items per line - nilamo - Nov-19-2017

Don't print the whole list all at once.  Either print each item in it one at a time, and use print()'s end= parameter to prevent the newline, or format the list into a string and print that.