#!/usr/bin/python


def outlierCleaner(predictions, ages, net_worths):
    """
        Clean away the 10% of points that have the largest
        residual errors (difference between the prediction
        and the actual net worth).

        Return a list of tuples named cleaned_data where 
        each tuple is of the form (age, net_worth, error).
    """
    
    cleaned_data = []
    for i in range(len(ages)):
        cleaned_data.append((ages[i], net_worths[i], abs(net_worths[i] - predictions[i])))
    
    cleaned_data.sort(key= lambda (a,b,c): c)
    end_index = int(round(len(ages) * .9)) 
    cleaned_data = cleaned_data[0:end_index]
    print len(cleaned_data)
    return cleaned_data

