How do Return only return first index from the list, Python3.9?

Asked

Viewed 27 times

-2

I need to get my Return to return all the values that are on my list, all in one function. But always returns only the first value.

Retornar valores de lista python3.x

  • 1

    Do not use images when posting your question. Always post code.

1 answer

1

The command return completes the function, so when you do the loop and puts the return within it, it returns only the first element of loop and then the function ends, the correct one is returns the set of values.

def returnAllValues(listOfValues):
    return listOfValues
 
def returnFirstValue(listOfValues):
    for value in listOfValues:
        return value
 
x = [1, 2, 3]
 
print(returnAllValues(x))
print(returnFirstValue(x))
Output:
[1, 2, 3]
1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.