Reputation: 35
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?
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']
Upvotes: 2
Views: 1414
Reputation: 413
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
Upvotes: 0
Reputation: 5237
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']
Upvotes: 2
Reputation: 567
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
Upvotes: 0
Reputation: 451
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'])
Upvotes: 0
Reputation: 3121
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']
Upvotes: 2