Python Forum

Full Version: number of items per line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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).
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.
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?
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.