Home > Software design >  Format a print output in a specific way
Format a print output in a specific way

Time:12-24

let's say I have a pre-described list

l = [2333, 1212, 1000, 5000, 3000, 4000, 7000, 2918, 7665, 1982, 2900]

And I already made a sorting algorithm to sort it out.

I made a for loop to iterate in the list so that it would print in this manner:

1000, 1212, 1982, 2333, 2900, 2918, 3000, 4000, 5000, 7000, 7665

My code goes like this:

for i in range(len(l)):
    
    print(l[i], end=" ")

However the output that I received was:

1000 1212 1982 2333 2900 2918 3000 4000 5000 7000 7665

How do I make it print to look like this:

1000, 1212, 1982, 2333, 2900, 2918, 3000, 4000, 5000, 7000, 7665

Here's a snippet of the sorting algorithm that I made:

l = [2333, 1212, 1000, 5000, 3000, 4000, 7000, 2918, 7665, 1982, 2900]

def myselection(list):
    for i in range(len(l)):
        minimum = i
        for j in range(i   1, len(l)):
            if l[minimum] > l[j]:
                minimum = j

        (l[i], l[minimum]) = (l[minimum], l[i])

myselection(list)

for i in range(len(l)):
    
    print(l[i], end=" ")

tried using join and map functions but it didn't work. I hope you can help me on this. Merry Christmas!

CodePudding user response:

print(", ".join(l))

That should print the list, seperated by commas

CodePudding user response:

Using sep

l = [2333, 1212, 1000, 5000, 3000, 4000, 7000, 2918, 7665, 1982, 2900]

def myselection(list):
    for i in range(len(l)):
        minimum = i
        for j in range(i   1, len(l)):
            if l[minimum] > l[j]:
                minimum = j

        (l[i], l[minimum]) = (l[minimum], l[i])

myselection(list)

print(*l, sep=',')

Output:-

1000, 1212, 1982, 2333, 2900, 2918, 3000, 4000, 5000, 7000, 7665
  • Related