-2

I want to read Python function output line by line without printing None object on screen

Created function called server_array, now using same function in for loop:

for i in [server_array()]:
    print (i)

But it's printing None too:

(venv) C:\myproject>python API\execution.py
/api/deployments/1360294004/server_arrays
/api/deployments/1360332004/server_arrays
/api/deployments/1360356004/server_arrays
/api/deployments/1360368004/server_arrays
/api/deployments/1360376004/server_arrays
/api/deployments/1360388004/server_arrays
/api/deployments/1360392004/server_arrays
/api/deployments/1360408004/server_arrays
/api/deployments/1360418004/server_arrays
/api/deployments/1360426004/server_arrays
/api/deployments/1360430004/server_arrays
/api/deployments/1360433004/server_arrays
/api/deployments/1360447004/server_arrays
/api/deployments/1360448004/server_arrays
/api/deployments/1360452004/server_arrays
/api/deployments/1360473004/server_arrays
None
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sagar
  • 1
  • 1
    Shown code prints what `server_array()` returns. The problem lies in this function. – Michael Butscher May 29 '20 at 20:50
  • 4
    Why did you expect that it wouldn't? Most likely `server_array` prints things itself and doesn't return anything, in which case maybe just **don't print `i`** (or, for that matter, don't build a single-element list, iterate over it and print each one thing in it. Just write `server_array()`). Read e.g. https://stackoverflow.com/q/7664779/3001761. – jonrsharpe May 29 '20 at 20:51
  • @jonrsharpe, i agree with you. So , is there any way to use for loop again on function output ? – Sagar May 30 '20 at 07:51
  • There is *if the function returns something iterable*. None is not iterable. – jonrsharpe May 30 '20 at 07:52
  • @jonrsharpe , i have achieved my resonse in below steps : Used `append` function to print all output in form of list and then `return` the same (to remove none) – Sagar May 30 '20 at 09:17

1 Answers1

0

Try this:

responses = [element for element in server_array() if element is not None]

Then either:

print(responses)

or

for curr_response in responses:
    print(curr_response)

This will get rid of your None, but other commenters are right in that maybe that function shouldn't tack on None to the end of its responses.

Good luck, Happy Coding!

Sam
  • 1,406
  • 1
  • 8
  • 11