0

I am getting extra whitespace after the end of output because I've used end=' '. How can I fix this?

for i in range(0,6):
    for g,h in q[i].items():
        print(h/len(q1[i]), end=" ") //here
    print()

enter image description here

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
Kaneki
  • 99
  • 11

3 Answers3

4

How about having just one print statement per line? You can use join to put spaces between the values.

for i in range(6):
    print(' '.join(str(h/len(q1[i])) for h in q[i].values()))
Blue Star
  • 1,932
  • 1
  • 10
  • 11
2
for i in range(0,6):
    print(*[h/len(q1[i]) for g,h in q[i].items()])
    print()

Using this answer, we can simply unpack the list to print everything without the inner for loop.

Example:

>>> print(*[1,2,3])
1 2 3
StardustGogeta
  • 3,331
  • 2
  • 18
  • 32
0

Use str.join() to put spaces in between your items:

for i in range(0,6):
    items = [str(h/len(q1[i])) for g,h in q[i].items()]
    print(" ".join(items))
quamrana
  • 37,849
  • 12
  • 53
  • 71