You could join the strings with a newline, and print the resulting string:
print "\n".join(op)
Answer from Sebastian Paaske Tørholm on Stack OverflowYou could join the strings with a newline, and print the resulting string:
print "\n".join(op)
For Python 3, we can also use list unpack
https://docs.python.org/3.7/tutorial/controlflow.html#unpacking-argument-lists
print(*op, sep='\n')
It's the same as
print('Hello', 'Good Morning', 'Good Evening', 'Good Night', 'Bye', sep='\n')
python - print list elements line by line - is it possible using format - Stack Overflow
python - Printing an int list in a single line python3 - Stack Overflow
How do I print every 3 values of an array in new lines?
python - How to print an array row by row? - Stack Overflow
How do I print an array in Python without brackets?
What is the difference between printing a list and a NumPy array?
How do I print a 2D array as a grid with aligned columns?
You can use more pythonic approach to achieve desired output.
file.writelines('\n'.join(applicants))
Your bug/issue was in your logic specifically in your else block.
for David your index is 1 so it will go into else block and in else block you are writing line as el+"\n" so David is being added on same line as Avery and then new line is added. I hope it cleared your doubt. So if in else block you can do file.write("\n"+el) and in elif too.
Another way can be:-
with open("Fiddle.txt","w") as file:
file.writelines("\n".join(applicants)) # will join elements of list with "\n"
will print names on new lines.
If you are using Python 3.x and your intention is to just printing the list of elements, one in each line, then you can use print function itself, like this
my_list = [1, 2, 3, 4]
print(*my_list, sep="\n")
*my_list simply unpacks the list elements and pass each one of them as parameters to the print function (Yes, print is a function in Python 3.x).
Output
1
2
3
4
If you are using Python 2.x, then you can just import the print function from the future like this
from __future__ import print_function
Note: This import should be the first line in the file.
You can use the string formatter on really any kind of string, including multi-line string. So of course, if you had a format string '{}\n{}\n{}' you could pass three items to it, and they would be all placed on separate lines.
So with a dynamic number of elements you want to print, all you need to do is make sure that the format string contains the same number of format items too. One way to solve this would be to construct the format string dynamically. For example this:
'\n'.join('{}' for _ in range(len(my_list))).format(*my_list)
So you essentially create a format string first, by having a generator produce one format item {} per element in my_list, and joining these using a newline character. So the resulting string looks something like this: {}\n{}\n…\n{}\n{}.
And then you use that string as the format string, and call format on it, passing the unpacked list as arguments to it. So you are correctly filling all spots of the format string.
So, you can do it. However, this is not really a practical idea. It looks rather confusing and does not convey your intention well. A better way would be to handle each item of your list separately and format it separately, and only then join them together:
'\n'.join('{}'.format(item) for item in my_list)
As for just printing elements line by line, of course, the more obvious way, that wouldn’t require you to build one long string with line breaks, would be to loop over the items and just print them one-by-one:
for item in my_list:
print(item)
# or use string formatting for the item here
print('{}'.format(item))
And of course, as thefourtheye suggested, if each loop iteration is very simple, you can also pass the whole list to the print function, and set sep='\n' to print the elements on separate lines each.
Yes that is possible in Python 3, just use * before the variable like:
print(*list)
This will print the list separated by spaces.
(where * is the unpacking operator that turns a list into positional arguments, print(*[1,2,3]) is the same as print(1,2,3), see also What does the star operator mean, in a function call?)
You want to say
for i in array:
print(i, end=" ")
The syntax i in array iterates over each member of the list. So, array[i] was trying to access array[1], array[2], and array[3], but the last of these is out of bounds (array has indices 0, 1, and 2).
You can get the same effect with print(" ".join(map(str,array))).
I do not want to manually specify.
row = [" "] * 9 print(row[0:3]) print(row[3:6]) print(row[6:9])
…is my current code.
In order to print a numpy.array into a single line, you can convert it to a list with its built-in function numpy.tolist()
Example:
import numpy as np
arr = np.array(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
Simple print of array:
print(arr)
[[1, 2, 3]
[4, 5, 6]
[7, 8, 9]]
In comparison with numpy.tolist():
print(array.tolist())
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
NumPy provides a few ways to customize the printing, for example np.array2string.
For this answer I assume you have such an array:
>>> import numpy as np
... arr = np.array([[ 0.15555605, 0.51031528, 0.84580176, 0.06722675],
... [ 0.60556045, 0.62721023, -0.48979983, -0.04152777],
... [-0.78044785, 0.58837543, -0.21146041, -0.13568023],
... [ 0. , 0. , 0. , 1. ]])
- If you want to display all items you need to make sure that you set
thresholdtonp.inf. - In case you want to have
,as separator, you can setseparatorto','.
However it doesn't have an option to remove the line breaks, just
max_line_widthwhich gives the number of characters printed in one line for the innermost dimension. So it works for 1D arrays when you setmax_line_width=np.infbut it doesn't work out of the box for ND arrays.
Fortunately it returns a string that can be manipulated, for example by removing all linebreaks:
>>> np.array2string(arr, threshold=np.inf, max_line_width=np.inf, separator=',').replace('\n', '')
'[[ 0.15555605, 0.51031528, 0.84580176, 0.06722675], [ 0.60556045, 0.62721023,-0.48979983,-0.04152777], [-0.78044785, 0.58837543,-0.21146041,-0.13568023], [ 0. , 0. , 0. , 1. ]]'
Or use a regular expression to remove all whitespaces:
>>> import re
>>> re.sub(r'\s+', '', np.array2string(arr, threshold=np.inf, max_line_width=np.inf, separator=','))
'[[0.15555605,0.51031528,0.84580176,0.06722675],[0.60556045,0.62721023,-0.48979983,-0.04152777],[-0.78044785,0.58837543,-0.21146041,-0.13568023],[0.,0.,0.,1.]]'
Agreed these aren't really "short" and they are also slower than converting to a list with .tolist() and then to a string but it's probably a good alternative especially if you want to customize the printed result without creating a (potentially huge) unnecessary list.