0

I'm new to coding and need to find the minimum value from a list of tuples.

def min_steps(step_records):
    """ random """
    if step_records != []:
        for steps in step_records:
            day, step = steps
            result = min(step)
    else:
        result = None
    return result 

This results in an error:

'int' object is not iterable

How do I return the min if the list is something like this?

step_records = [('2010-01-01',1),
                ('2010-01-02',2),
                ('2010-01-03',3)]
Anthon
  • 69,918
  • 32
  • 186
  • 246

1 Answers1

0

tuples can be indexed (see: Accessing a value in a tuple that is in a list).

Using that we can create a list from those indices and call minimum like you had done:

def min_steps(step_records):
    """ random """
    if step_records:
        result = min([step[1] for step in step_records]) # min([1,2,3])
    else:
        result = None
    return result

step_records = [('2010-01-01',1),
                ('2010-01-02',2),
                ('2010-01-03',3)]

print(min_steps(step_records))

output:

1