I there a way to return all my elements of a list?
def singular(list):
for i in range(len(list)):
print(i)
a = singular(list)
print(a)
with print(i) i get after all elements None as value with return i only the first value
I there a way to return all my elements of a list?
def singular(list):
for i in range(len(list)):
print(i)
a = singular(list)
print(a)
with print(i) i get after all elements None as value with return i only the first value
None
is not returned as the last value from your list. It gets printed from your print(a)
statement, and it is printing None
, because the function doesn't return
anything. There is the code that possibly outputs what you want:
my_list = [1, 2, 3, 4]
def singular(passed_list): # Dont use 'list' as variable name
for i in range(len(passed_list)):
print(i)
return passed_list
a = singular(my_list)
print(*a)