FCF_margin2017 = 3.3 FCF_margin2018 = 1.5 FCF_margin2019 = 2.3 FCF_margin2020 = 30 fcf_margin_list = FCF_margin2017, FCF_margin2018, FCF_margin2019, FCF_margin2020 sorted_fcf_margin_list = sorted(fcf_margin_list) value1 = sorted_fcf_margin_list[0] value2 = sorted_fcf_margin_list[1] value3 = sorted_fcf_margin_list[2] value4 = sorted_fcf_margin_list[3] value_list = value1, value2, value3, value4 median_list = value2, value3 median = sum(median_list)/2 mean = sum(value_list)/2 upper = median+mean lower = median-mean def CheckForLess(fcf_margin_list, val): for x in fcf_margin_list: if val <= lower: if (CheckForLess(fcf_margin, val)): fcf_margin_list.remove() for x in fcf_margin_list: if val >= upper: if (CheckForLess(fcf_margin, val)): fcf_margin_list.remove() fcf_margin_list
This code currently returns:
(3.3, 1.5, 2.3, 30)
This is same as the original list, when I was trying to remove values in the list greater than upper and less than lower. How can I go about removing values from the list fcf_margin_list
given that they meet my specified requirements?
Answer
I would probably create a new list containing just the items I want.
new_list = [item for item in original_list if item >= lower and item <= upper]
I could always replace the original list with the new list if I wished to do so,
original_list = new_list