my_list = ['a','b','c']
def func(input):
for i in input:
print i
print func(my_list)
Output
a
b
c
None
I don't want the 'None' so how I can do a single line of code to return the items in my input list?
I tried:
return for i in input: print i
but it didn't work because return statements don't work like that
Functions, by default, return
None
if they come to the end of themselves without returning. So, since there are noreturn
statements infunc
, it will returnNone
when called.Furthermore, by using
print func(my_list)
, you are telling Python to print what is returned byfunc
, which isNone
.To fix your problem, just drop the
print
and do:Also, if
my_list
isn't very long, then you don't really need a function at all to do what you are doing. Just use thejoin
method of a string like so:You are printing the return code from
func
which isnone
. Just callfunc(my_list)
without theprint
.You don't need to call
print func(my_list)
- just do: