Hi,
I've been trying to use reduce() function to add values in the list of tuples. I'm only interested to add and display the numbers, but I don't know how to access and operate only on the second column(numbers)
I've tried to do it using regular function and then the reduce()
What am I doing wrong?
I've been trying to use reduce() function to add values in the list of tuples. I'm only interested to add and display the numbers, but I don't know how to access and operate only on the second column(numbers)
I've tried to do it using regular function and then the reduce()
What am I doing wrong?
import functools list = [("Rachel",19), ("Monica",18), ("Phoebe",17), ("Joey",16), ("Ross",20), ("Chandler",21)] def func(self): x = list[0][1] y = list[1][1] return x+y func_x = func(self="") print(func_x) ages_added = functools.reduce(lambda x,y:x[1][1]+y[1][1],list) print(ages_added)
Larz60+ write Jan-05-2023, 12:28 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button
Fixed for you this time. Please use BBCode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button
Fixed for you this time. Please use BBCode tags on future posts.