-1

How can I print a list without brackets and commas?

n1 = 15.3
n2 = 18.2
name = 'Ana'
l = [[name], ['Grade 1: ', round(n1)], ['Grade 2: ', round(n2)]]
print(l)

I get:

[['Ana'], ['Grade 1: ', 15], ['Grade 2: ', 18]]

But I need it like this:

Ana 
Grade 1: 15 
Grade 2: 18
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

4 Answers4

1

You have a list of lists, so other questions such as print list without brackets don't directly apply.

So loop over the list using for, and print each sub-list in turn:

for x in l:
    print(*x, sep='')
Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
0

Try This :)

n1 = 15.3
n2 = 18.2
name = "Ana"
list = [name, f"Grade 1: {round(n1)}", f"Grade 2: {round(n2)}"]
for thing in list:
    print(f"{thing}")

Your problem is that inside a list [] You just need to type the variable or string and a comma afterwards. You don't need to put them in brackets or quotes.

  • I tried if the f and {}, to format it, it worked better, but I still got the number with {}, like: {15} {18}. Then I tried without the list within the list, I removed all the [ ] and it worked Thanks for the tip! – Matheus Rosa Mar 19 '20 at 18:31
0

Print using list comprehension and a star expression * to evaluate the list as an iterable.

[print(*item) for item in l]

The star expression (*args)is documented in the Expressions reference:

If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments x1, ..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM.

Lee James
  • 91
  • 1
  • 5
  • List comprehensions are not meant for side effects. i.e. you're creating a list only to print something and left with a list full of `None` that you never use. This case requires a simple loop as already suggested in other answers. List comprehensions are for when you actually need to construct a list – Tomerikoo Jun 21 '21 at 15:44
-1

Little changes

n1 = 15.3

n2 = 18.2

name= 'Ana'

l = [name,'Grade 1: {}'.format(round(n1)),'Grade 1: {}'.format(round(n2))]

that .format will be useful for you

added formula:

for things in l:

print(things)
Community
  • 1
  • 1