简体   繁体   中英

Different Output in Print and Return in Python Function

I want to return the words from a list whose first letter starts with s and I have performed the following 2 solutions. One is near to the solution and the other one is correct but not in the exact form which is desired. And also I am getting a different result if I use "print" vs "return" in python function. Why is that so? Please guide me.

1st Method:

def s(opt):
    for a in opt:
        if a[0] == 's':
            return(a)
s(['soup','dog','salad','cat','great'])

Output I am getting by running this code is 'soup' - that too in inverted commas - Why?

2nd Method:

def s(opt):
    for a in opt:
        if a[0] == 's':
            print(a)
s(['soup','dog','salad','cat','great'])

Output I am getting by this method is soup, salad in vertical form with no inverted commas and all. Why?

My question is how can I get the desired output by keeping the return in function method? Another question why output is being different when used print vs return in above methods?

Desired Output: ['soup', 'salad']

Use yield instead of return . When your condition if a[0] == 's' is getting true then the s() function return.

If you want to return multiple values when your requirements meet then you have to use another list to store your answer or you can use list comprehension

def s(opt):
    for a in opt:
        if a[0] == 's':
            yield a


print(list(s(['soup', 'dog', 'salad', 'cat', 'great'])))

# Output
# ['soup', 'salad']

Your version using print works because it merely prints the values as they are found, and does not return from the function until the loop is over. It is less useful since it returns None , so you can't make use of the result itself -- you can only see printed output.

Your return statement will return the first element matching that condition. It will not proceed to check further elements in the list after that point. You need to construct a list of return values instead:

def s(opt):
    result = []
    for a in opt:
        if a[0] == 's':
            result.append(a)
    return result

print(s(['soup', 'dog', 'salad', 'cat', 'great']))

This code uses list.append to add the elements to the result list one by one. Then, at the end of the function, it returns the result .

Or, use a list comprehension :

def s(opt):
    return [a for a in opt if a[0] == 's']

print(s(['soup', 'dog', 'salad', 'cat', 'great']))

In both cases, the output is as desired:

['soup', 'salad']

This is because once you return a value in a function, the function stops. Once it returns soup, the whole functions stops, but when you print each out, you get all of the results. Here's a solution to your problem:

def s(opt):
    answers = []
    for a in opt:
        if a[0] == 's':
            answers.append(a)
    return(answers)
s(['soup','dog','salad','cat','great'])
def s(opt):
    matches = []
    for a in opt:
        if a[0] == 's':
            matches.append(a)
    return matches

s(['soup','dog','salad','cat','great'])

this should give your desired output

  1. use return the loop is stop and return the value "a" on the otherwise "print" will not stop the loop and keep going
  2. func return is diff, print is just to show the value,return will return the value u could use later
def s(opt):
    for a in opt:
        if a[0] == 's':
            return(a)
# r is soup
r = s(['soup','dog','salad','cat','great'])

def s(opt):
    for a in opt:
        if a[0] == 's':
            print(a)
s(['soup','dog','salad','cat','great'])
# z is None
z = s(['soup','dog','salad','cat','great'])

i suggest u use list contain the value u want and return

def s(opt):
    l = []
    for a in opt:
        if a[0] == 's':
            l.append(a)
    return l

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM