Python iterating through pair of values in list for function
Python iterating through pair of values in list for function
I have a list like
cases = [(1,1), (2,2), (3,3)]
trying to write a function that calculates through each item and return values:
def case_cal(cases)
for x, y in cases:
result = x+y
return result
output = case_cal(cases)
print(output)
I like to get output like
2
4
6
But I only get
2
I am a newbie learning python and something simple I am missing here. Could I get any advice? Thanks in advance!
return
6 Answers
6
Once you return something you move out of the function. So make a list, append your values to the list and then return in the end.
def case_cal(cases):
ret_values =
for x, y in cases:
result = x+y
ret_values.append(result)
return ret_values
output = case_cal(cases)
print(*output)
Your function body could also be written
return [x + y for x, y in cases]
.– ggorlen
Aug 25 at 2:55
return [x + y for x, y in cases]
@ggorlen, yes, indeed. But as the OP seems to be a beginner, didn't want to confuse him with comprehensions.
– Deepak Saini
Aug 25 at 2:57
Definitely, I like this answer quite a bit.
– ggorlen
Aug 25 at 2:57
That worked, Thanks for the quick response!
– lukas
Aug 25 at 3:16
Your code return
s inside the for
loop, at the end of the first iteration, so you'll only see the first x + y
result.
return
for
x + y
This is a perfect use for a generator, which will allow you to grab the next x + y
calculation on demand and offer maximum control over what the caller can do with the result:
x + y
cases = [(1,1), (2,2), (3,3)]
def case_cal(cases):
for x, y in cases:
yield x + y
for x in case_cal(cases):
print(x)
Output:
2
4
6
And a repl to play with.
Using
list
comprehensions for side-effects is evil (it's a functional programming tool; true functional programming is naturally side-effect free, and people don't tend to expect side-effects in functional constructs even in multiparadigm languages). It also needlessly creates list
s of None
(minor performance cost, and in a repl
, it means echoing out that list
of None
for bonus confusion). for x in case_cal(cases): print(x)
is equally simple, and not willfully evil. :-)– ShadowRanger
Aug 25 at 2:47
list
list
None
repl
list
None
for x in case_cal(cases): print(x)
I'm not sure I buy that Python has much relation to "true functional programming", but I accept the argument that it creates a list of
None
unnecessarily. Will adjust!– ggorlen
Aug 25 at 2:50
None
You can simply map the items of the list to the sum
function:
sum
list(map(sum, cases))
This becomes:
[2, 4, 6]
Or if you want to print the items individually:
for s in map(sum, cases):
print(s)
This outputs:
2
4
6
If you dont need the values in a list you can procced as follows:
cases = [(1,1), (2,2), (3,3)]
def case_cal(cases):
for x, y in cases:
result = x+y
print(result)
case_cal(cases)
Just directly print the values instead of returning them as once you return something you move out of the function.
Also,
cases = [(1,1), (2,2), (3,3)]
def case_cal(cases):
for x, y in cases:
print(x+y)
case_cal(cases)
Writing a function that has side effects when OP is trying to write a function with no side effects seems to avoid the problem--it's fundamentally a different function.
– ggorlen
Aug 25 at 2:42
You could do this:
cases = [(1,1), (2,2), (3,3)]
def case_cal(cases):
results =
for x, y in cases:
results.append(x + y)
return results
output = [case_cal(cases)]
print(output)
And then you could define a print function like this:
def print_cases(cases):
for elem in cases[0]:
print(elem)
But there is a more Pythonic way:
def case_cal(cases):
return [(x + y) for (x, y) in cases]
This is called a list comprehension, and you are going to be using them very often in python. You can read more about them here.
Thanks for the link on the list comprehension! Helps me to understand more about Python as I am teaching myself right now.
– lukas
Aug 27 at 5:14
Simply do the following:
def case_calc(cases):
for x, y in cases:
print(x + y)
case_calc([(1, 1), (2, 2), (3, 3)])
Cheers
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
return
exits the function immediately, so you only get the the result of 1+1.– John Gordon
Aug 25 at 2:34