Python Forum
Adding values with reduce() function from the list of tuples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding values with reduce() function from the list of tuples
#1
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?

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.
Reply


Messages In This Thread
Adding values with reduce() function from the list of tuples - by kinimod - Jan-05-2023, 11:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [redistribution] Reduce number + size of dependencies? Winfried 2 757 Jan-31-2025, 10:17 PM
Last Post: snippsat
  Assigning cycle values in a list nmancini 3 1,124 Sep-16-2024, 09:35 PM
Last Post: deanhystad
  remove duplicates from dicts with list values wardancer84 27 6,545 May-27-2024, 04:54 PM
Last Post: wardancer84
  Copying the order of another list with identical values gohanhango 7 2,860 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 3,025 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 3,694 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  reduce nested for-loops Phaze90 11 5,461 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  user input values into list of lists tauros73 3 2,086 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  function accepts infinite parameters and returns a graph with those values edencthompson 0 1,400 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 21,505 Jan-19-2022, 08:33 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020