-1

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

  • Your code does not update anything. It just prints stuff out. – PM 77-1 Jan 16 '21 at 18:21
  • `print` is not the same as `return`. – iBug Jan 16 '21 at 18:21
  • Your function doesn't return anything so why are you trying to print it? – Brian O'Donnell Jan 16 '21 at 18:22
  • Does this answer your question? [How is returning the output of a function different from printing it?](https://stackoverflow.com/questions/750136/how-is-returning-the-output-of-a-function-different-from-printing-it) – PM 77-1 Jan 16 '21 at 18:23
  • ah, so because return needs for updating a structure. k. and in a function - just printing values out -without None as last print value - is therefore not possible. correct? – pythonabsir Jan 16 '21 at 18:42
  • This should instead be a duplicate of [How can I use `return` to get back multiple values from a for loop? Can I put them in a list?](https://stackoverflow.com/questions/44564414/). – Karl Knechtel Aug 15 '22 at 04:30

1 Answers1

1

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)
MercifulSory
  • 337
  • 1
  • 14
  • i wanna just print my numbers of my list. - without the list. - so it isnt possible without -None- as last value? yes, i know my function doesnt return anything, because i wanted to avoid to just return a new list [with, numbers] – pythonabsir Jan 16 '21 at 18:32
  • Edited. Check it out – MercifulSory Jan 16 '21 at 18:40