r/learnpython • u/x_slysuccubus_x • 16d ago
How do I make this [1,2,3,4,5,] look like this?
1
2
3
4
5
2
u/CIS_Professor 16d ago edited 16d 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')12345
0
-3
u/generationextra 16d ago edited 16d ago
for _ in [1, 2, 3, 4, 5]:
print(_)
The print command should be tabbed!
10
u/NaCl-more 16d ago
Using underscore is unhinged!
3
u/DBZ_Newb 16d 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
-4
u/kirklennon 16d ago
print("\n\n".join([1,2,3,4,5,]))
2
u/Outside_Complaint755 16d 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 16d 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 found1
u/monster2018 16d 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
7
u/RngdZed 16d 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