1

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:

  1. In the first iteration when index: 0 and element:0, input_list = [1, 3, 8, 13]
  2. In the next iteration, I want the values to be like this - index: 0 and element: 1 and input_list = [1, 3, 11]
  3. 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!

1 Answer 1

1

In the for loop, you keep assigning to input_list a new list object:

input_list = [x-2 for x in input_list if (x-2)>0]

while the iterator of the for loop is based on the original list object, and it therefore would not reflect the new list objects assigned to input_list.

You can instead alter input_list in-place by slicing its entire range:

input_list[:] = [x-2 for x in input_list if (x-2)>0]

so that the iterator can reflect changes made to the same list object.

Sign up to request clarification or add additional context in comments.

2 Comments

When I tried the code, the index goes on increasing as the input_list values change. It is using the new values of the input_list, but I wanted the iteration to start from index:0 again as I have shown in the explanation of how I want the loop to iterate. I know that this may not be possible using enumerate. But I'm looking for a way of doing this. It would be great if your solution could do this!
That's an extremely custom behavior you're describing then. You would certainly have to create your own class that implements Python's iterator protocol to produce the kind of iterable you're looking for. Please make your own attempt and, if you're stuck, post another question specific to the new code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.