Home > OS >  Output of a function line by line not as a list python
Output of a function line by line not as a list python

Time:07-16

I have written a function and its output is returned as a list,

[32,1,4,5,6]

But I want it to return in this manner,
32
1
4
5
6

How do I return the output as above?

CodePudding user response:

Loop through the list and get the result, as:

result = list(range(10)) # your lists values
for i in result:
    print(i)

or you can use:

print(*result, sep='\n')

using functions

>>> def func(n):
...     for i in range(n):
...             yield i
... 
>>> for i in func(10):
...     print(i)
... 
0
1
2
3
4
5
6
7
8
9
>>> def func2(n):
...     result = []
...     for i in range(n):
...             result.append(i)
...     return '\n'.join(str(i) for i in result)
... 
>>> func2(10)
'0\n1\n2\n3\n4\n5\n6\n7\n8\n9'
>>> x = func2(10)
>>> print(x)
0
1
2
3
4
5
6
7
8
9

CodePudding user response:

my_list = the_function_that_returns_a_list()

for item in my_list:
    print(item)

CodePudding user response:

you can add the following code for getting required output

string = ""

for i in result : string = i "/n"

  • Related