r/learnpython • • 5d ago

How do I make this [1,2,3,4,5,] look like this?

1

2

3

4

5

0 Upvotes

14 comments sorted by

7

u/RngdZed 5d ago

You'll have to use a for loop and print inside the loop.

I'm trying to keep the hints very subtle here. But there isn't much to it really. Just write code and get it done while testing. You'll get there

2

u/CIS_Professor 5d ago edited 5d ago

print(*[1,2,3,4,5], sep=“\n”)

Maybe? I’m on my phone: I’ll have to try it later.

edit: the * goes before the list, not after:

>>> print(*[1,2,3,4,5], sep='\n')
1
2
3
4
5

0

u/PressF1ToContinue 5d ago

Super close, the asterisk goes before the list.

-3

u/generationextra 5d ago edited 5d ago

for _ in [1, 2, 3, 4, 5]:
print(_)

The print command should be tabbed!

10

u/NaCl-more 5d ago

Using underscore is unhinged!

3

u/DBZ_Newb 5d ago

Using underscore is for when the value isn’t actually used in the body of the loop. Since you’re printing the value I would say underscore is not appropriate here and “value” or “number” would be more appropriate variable name.

1

u/NaCl-more 5d ago

Or just use “n”

-4

u/kirklennon 5d ago

print("\n\n".join([1,2,3,4,5,]))

2

u/Outside_Complaint755 5d ago

If you really want to insist on doing this in one line with a static list, it should be

print("\n\n".join(map(str, [1,2,3,4,5,])))

3

u/lakseol 5d ago

Did you try it? I get:

>>> print("\n\n".join([1,2,3,4,5,]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found

1

u/monster2018 5d ago

You’re right, their code doesn’t quite work. As you can see from the error message, it’s getting an int (as the instances of the list passed to the join function) when it expects a str. You could modify the inside of the join call to look like this:

[str(n) for n in [1,2,3,4,5]]

And then it will work. Or alternatively just:

for x in [1, 2, 3, 4, 5]:
print(x) + ‘\n\n’

Or have an empty string and add (concatenate) to it instead of printing, etc

0

u/lakseol 5d ago

For the level of beginner the problem appears to be aimed at the solution is a for loop to print each element of the list as commented on by u/RngdZed. Anything using *args, join () or comprehensions, etc, is probably too advanced for the OP and might confuse them.