Python Forum
number of items per line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
number of items per line
#1
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).
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding Row Number for Items in 2D array fafzal 2 2,406 Jan-10-2019, 06:11 AM
Last Post: fafzal

Forum Jump:

User Panel Messages

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