Format a print output in a specific way

Question:

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!

Asked By: dedpolx

||

Answers:

print(", ".join(l))

That should print the list, seperated by commas

Answered By: Coollector

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
Answered By: Yash Mehta
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.