user15344209
user15344209

Reputation:

Python code not displaying the list values each in a new line no matter how hard I try

I've written a code which works pretty well, no errors, no problems. But no matter how hard I try to print the list values each in a new line, it still wouldn't work. I've tried sep='\n', and even tried to put the list in a loop to print each value one by one. I still get the result printed all in a row, in one line. This sounds too annoying and I can't figure out why my code is having this strange behavior. Here's my code:

length = int(input())
input_string = [int(y) for y in input().split()]

def lowest(string):

    return(min(x for x in string if x is not None))

def none_set(string):
    for k in range(length):
        if string[k] != None:
            if string[k] <=0:
                string[k] = None

def counter(string):
    nums = 0
    for h in range(length):
        if string[h] != None:
            nums += 1

    return nums

cnt = []

for i in range(length):   
    minimum = lowest(input_string)
    none_set(input_string) 
    
    cnt.append(counter(input_string))

    for j in range(length):
        if input_string[j] != None:
            input_string[j] -= minimum
        
result = list(set(cnt))[::-1]
print(result, sep='\n') #Doesn't print the values in new line :/

Sample Input:

6
5 4 4 2 2 8

Expected Output:

6
4
2
1

The Output I Get:

[6, 4, 2, 1]

In case you want to know what exactly my code does, check this link here (No login/signup needed), however, the issue is not really relative to what the goal of my code is, I'd say.

I appreciate in advance, for any tip, solution, or help.

Upvotes: 0

Views: 561

Answers (4)

Michael Ruth
Michael Ruth

Reputation: 3504

Use join() instead of multiple calls to print(). Some will consider it more difficult to read, but it's definitely more efficient even for small lists, and orders of magnitude faster for lists greater than 100 elements.

print("\n".join(map(str, result)))

Upvotes: 0

Eitan Rosati
Eitan Rosati

Reputation: 553

Try to print like that:

for i in result:
    print(i)

Upvotes: 0

Kevin G
Kevin G

Reputation: 307

Try this.

 for i in result:
     print(i)

Upvotes: 0

Have a nice day
Have a nice day

Reputation: 1005

It's because you're printing the whole list.

lst = ['a','b','c']

If I print this list I get ['a','b','c']. To print each item you can use a for loop like so:

lst = ['a','b','c']

for item in lst:
    print(item)

#output: 
a
b
c

Upvotes: 1

Related Questions