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
#2
Firstly, the name of the list should not be named list because list is a built-in function.
To get the second values from the tuple, you must iterate over the data.
data = [
    ("Rachel", 19),
    ("Monica", 18),
    ("Phoebe", 17),
    ("Joey", 16),
    ("Ross", 20),
    ("Chandler", 21),
]


for elements in data:
    print(elements)
    # ("Rachel",19) ...

for elements in data:
    print(elements[1])  # print second element

for name, age in data:
    print(age)
    # but name is not used, so you can assgn it the the thow away variable _


for _, age in data:
    print(age)
Or if you want to create a new list only with the second values of the tuples:
from operator import itemgetter

def get(data, index):
    getter = itemgetter(index)
    return [getter(values) for values in data]

# without itemgetter
def get(data, index):
    return [values[index] for values in data]


data = [
    ("Rachel", 19),
    ("Monica", 18),
    ("Phoebe", 17),
    ("Joey", 16),
    ("Ross", 20),
    ("Chandler", 21),
]

ages = get(data, 1)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Adding values with reduce() function from the list of tuples - by DeaD_EyE - Jan-05-2023, 12:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,186 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,284 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,206 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  reduce nested for-loops Phaze90 11 1,951 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  user input values into list of lists tauros73 3 1,086 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  function accepts infinite parameters and returns a graph with those values edencthompson 0 878 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,081 Jan-19-2022, 08:33 AM
Last Post: menator01
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,139 Jan-09-2022, 02:39 AM
Last Post: Python84
  List of dataframe values beginning with x,y or z glidecode 3 1,963 Nov-08-2021, 10:16 PM
Last Post: glidecode
  How to pass list of values to a API request URL chetansaip99 0 3,548 Sep-28-2021, 07:37 AM
Last Post: chetansaip99

Forum Jump:

User Panel Messages

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