Python Forum
List Alignment Help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List Alignment Help (/thread-630.html)



List Alignment Help - A3G - Oct-25-2016

Hello Friends,

I would like to get my list to print out horizontally and have spaces in between on a new line like this:

10 15 25 30
25 30 45 60
81 92 81

The last line does not need to be separated evenly like the other two. The user is able to enter in any amount.

Here is the code: 

def Grades(amount):
    import random
    for i in range(0, amount):    
        empty_list = []
        grade_list = amount*empty_list
        comb_list = grade_list.append(random.randint(0,100))
        print(grade_list)

#str_create = str(Grades(5))
#print(len(str_create))
        
def Rows(grade_list):
    grade_list_string = str(Grades(10))
    for i in range(0, len(grade_list_string)):
        pos_slice = grade_list_string[0:2]
        print(pos_slice)

Rows(1)
 


Thank you in advance! =)


RE: List Alignment Help - Ofnuts - Oct-25-2016

In Python3 you can use the "end" keyword parameter of the print() function: set it to a space between most numbers and to a newline every N numbers:
for i,n in enumerate(range(10,30)):
    print(n,end=' ' if (i+1)%5 else '\n')
Output:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
This way you don't have to explicitly create sublists just to print them.