Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Alignment Help
#1
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! =)
Reply
#2
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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 326 Jan-14-2024, 03:56 AM
Last Post: 1418
  make all text in Right to left alignment in tkinter widgets jalal0034 1 1,340 Sep-27-2022, 06:42 PM
Last Post: Larz60+
  Right to left alignment in python report using Reportlab jalal0034 1 1,847 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  pandas, tabulate, and alignment menator01 3 7,314 Feb-05-2022, 07:04 AM
Last Post: menator01
  Space between list and column alignment rturus 8 5,135 Mar-17-2021, 04:47 PM
Last Post: rturus
  Data alignment in Python Nirmal 1 2,644 Feb-12-2019, 09:55 PM
Last Post: nilamo
  OpenPyxl Cell.value Alignment pcsailor 0 8,833 Sep-10-2018, 01:09 AM
Last Post: pcsailor
  matplotlib barh y tick alignment iFunKtion 2 7,625 Oct-06-2016, 11:46 AM
Last Post: iFunKtion

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020