md2614
md2614

Reputation: 489

Get a list of maximum values per tuple from a list of tuples

I'm just getting into Python, and having some trouble understanding the control flow and iteration logic.

I am trying to create a function which takes a list of tuples, and I want to return a new list with the maximum element per tuple.

I know I'm missing putting the maximum element into a new list, but first I am trying to get that maximum value.

def max_element_per_tuple(tuple_list):
    maximum = tuple_list[0]

    for item in tuple_list:
        if item > maximum:
            maximum = item

    return maximum

# test it
tuple_list = [(-1,0,1), (10,20,30), (100,50,25), (55,75,65)]
print(max_element_per_tuple(tuple_list))

This returns: (100, 50, 25)

Want returned: (1, 30, 100, 75)

Or if a list (?), then: [1, 30, 100, 75]

Upvotes: 1

Views: 1202

Answers (5)

Błotosmętek
Błotosmętek

Reputation: 12927

Your function max_element_per_tuple is correct (though unnecessary, because standard function max() exists). What you did wrong was calling that function using a list of tuples as the argument. This found the biggest tuple in the list ("biggest" for tuples means "the one with first element biggest"), which happened to be the third one - (100,50,25). What you need to do is either:

result = list(map(max, tuple_list))

or

result = [max(t) for t in tuple_list]

This last one is roughly equivalent to:

result = []
for t in tuple_list:
    result.append(max(t))

If you replace max with your max_element_per_tuple the results should be the same.

Upvotes: 2

Tomerikoo
Tomerikoo

Reputation: 19430

Right now you are just looping through the tuples and returning the "biggest" one - tuple comparison is done element-wise.

What you want is to add another loop level that will find the maximum of each tuple:

def max_element_per_tuple(tuple_list):
    res = []

    for tup in tuple_list:      # loops over the tuples in the list
        maximum = tup[0]
        for item in tup:        # loops over each item of the tuple
            if item > maximum:
                maximum = item
        res.append(maximum)

    return res

This gives as expected:

>>> max_element_per_tuple([(-1, 0, 1), (10, 20, 30), (100, 50, 25), (55, 75, 65)])
[1, 30, 100, 75]

Upvotes: 2

Funwie
Funwie

Reputation: 91

The issue: max_element_per_tuple(tuple_list) returns the wrong result because it is looking for the max tuple, not the max value in each tuple.

def max_element_per_tuple(tuple_list):
    maximum = tuple_list[0]  # maximum is = (-1,0,1)

    for item in tuple_list:
        if item > maximum:  # compares the tuples e.g (-1,0,1) == (10,20,30)
            maximum = item

    return maximum  # at the end you have (100,50,25). it's the max tuple

Try any of below options:

tuple_list = [(-1,0,1), (10,20,30), (100,50,25), (55,75,65)]

# Get the max from each tuple using List comprehension
max_items_list  = [max(tuple_item) for tuple_item in tuple_list]   # in a list
max_items_tuple = tuple(max(tuple_item) for tuple_item in tuple_list)   # in a tuple
print(max_items_list)
print(max_items_tuple)

# Get the max from each tuple using For Loop
# Can be used with List only, tuples are immutable

for_max_items_list = list()

for tuple_item in tuple_list:
  max_value = max(tuple_item)  # get the max of each tuple e.g Max((-1,0,1)) = 1
  for_max_items_list.append(max_value)  # add the max to list
print(for_max_items_list)

Upvotes: 1

itismoej
itismoej

Reputation: 1847

Simply, try this one-linear pythonic solution

>>> tuple_list = [(-1,0,1), (10,20,30), (100,50,25), (55,75,65)]
>>> [max(e) for e in tuple_list]       # List
[1, 30, 100, 75]
>>> tuple(max(e) for e in tuple_list)  # Tuple
(1, 30, 100, 75)

Upvotes: 6

thebadguy
thebadguy

Reputation: 2140

This should work

def max_element_per_tuple(tuple_list):
    maximum = []

    for item in tuple_list:
        maximum.append(max(item))


    return maximum

will give this output : [1, 30, 100, 75]

Upvotes: 1

Related Questions