My code looks like this :
input_list = [0,3,5,7,15]
def sample_fun(input_list):
for idx,ele in enumerate(input_list):
input_list = [x-2 for x in input_list if (x-2)>0]
print('Index:',idx,'element:',ele,'New list:',input_list)
sample_fun(input_list)
What I am trying to show is that the value of input_list that is used inside of enumerate keeps changing inside the for loop. I want the for loop to iterate through the new value of input_list. But it appears that the for loop iterates through the initial value of input_list even though I am changing it's values.
Index: 0 element: 0 New list: [1, 3, 5, 13]
Index: 1 element: 3 New list: [1, 3, 11]
Index: 2 element: 5 New list: [1, 9]
Index: 3 element: 7 New list: [7]
Index: 4 element: 15 New list: [5]
I understand that the for loop is iterating through the initial enumerate output. Is there any way I could make the for loop iterate through the new values of input_list like:
- In the first iteration when
index: 0 and element:0, input_list = [1, 3, 8, 13]
- In the next iteration, I want the values to be like this -
index: 0 and element: 1 and input_list = [1, 3, 11]
- In the next iteration, I want the values to be like this -
index: 0 and element: 1
, now since the element is same as previous element value, I would like to loop through to -index:1 and element : 3 and input_list = [1, 9]
I want the loop to behave in this way.
I want to loop through the changing values of the input_list. I'm not sure how to do this. It would be great if anybody could help me out here. Thanks in advance!