JavaScript
x
4
1
with open('output.txt', 'w') as f:
2
for item in winapps.list_installed():
3
print(item, file=f)
4
So I have this basic code. How could I make it into line? without including the with open, as other stuff is included later on.
I was thinking something like this xD
JavaScript
1
3
1
with open('output.txt', 'w') as f:
2
for item in winapps.list_installed(print(item, file=f))
3
Advertisement
Answer
This can be put into one line in several ways. Without changing your code you could just remove the newline and indent:
JavaScript
1
3
1
with open('output.txt', 'w') as f:
2
for item in winapps.list_installed(): print(item, file=f)
3
Or just using unpacking and print formatting:
JavaScript
1
3
1
with open('output.txt', 'w') as f:
2
print(*winapps.list_installed(), sep="n", file=f)
3
Which can also be done in one line:
JavaScript
1
2
1
with open('output.txt', 'w') as f: print(*winapps.list_installed(), sep="n", file=f)
2
That said, this is not a good code design choice. Making things more conscience can (at times) make it less readable.