Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, Note that indexes in python start from 0, so the indexes for your example list are 0 to 4 not 1 to 5. You may also like to read the following Python tutorials. To create a numpy array with zeros, given shape of the array, use numpy.zeros () function. How to loop with indexes in Python - Trey Hunner Please see different approaches which can be used to iterate over list and access index value and their performance metrics (which I suppose would be useful for you) in code samples below: See performance metrics for each method below: As the result, using enumerate method is the fastest method for iteration when the index needed. enumerate () method is the most efficient method for accessing the index in a for loop. You may want to look into itertools.zip_longest if you need different behavior. The above codes don't work, index i can't be manually changed. Let us see how to control the increment in for-loops in Python. enumerate() is a built-in Python function which is very useful when we want to access both the values and the indices of a list. The function passed to map can take an additional parameter to represent the index of the current item. start (Optional) - The position from where the search begins. This PR updates coverage from 4.5.3 to 7.2.1. Update coverage to 7.2.1 #393 - github.com Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2, Traverse a list in reverse order in Python, Loop through list with both content and index. Changing the index temporarily by specifying inplace=False (or) we can make it without specifying inplace parameter because by default the inplace value is false. document.write(d.getFullYear()) @calculuswhiz the while loop is an important code snippet. As you can see, in each iteration of the while loop i is reassigned, therefore the value of i will be overridden regardless of any other reassignments you issue in the # some code with i part. Simple idea is that i takes a value after every iteration irregardless of what it is assigned to inside the loop because the loop increments the iterating variable at the end of the iteration and since the value of i is declared inside the loop, it is simply overwritten. Just as timgeb explained, the index you used was assigned a new value at the beginning of the for loop each time, the way that I found to work is to use another index. Here, we are using an iterator variable to iterate through a String. This is the most common way of accessing both elements and their indices at the same time. The easiest way to fix your code is to iterate over the indexes: It is not possible the way you are doing it. Example 1: Incrementing the iterator by 1. foo = [4, 5, 6] for idx, a in enumerate (foo): foo [idx] = a + 42 print (foo) Output: Or you can use list comprehensions (or map ), unless you really want to mutate in place (just don't insert or remove items from the iterated-on list). This method adds a counter to an iterable and returns them together as an enumerated object. If no parameters are passed, it returns an empty list, and if an iterable is passed as a parameter it creates a list consisting of its items. Ways to increment Iterator from inside the For loop in Python There is "for" loop which is similar to each loop in other languages. AC Op-amp integrator with DC Gain Control in LTspice, Doesn't analytically integrate sensibly let alone correctly.