How to Modify a List While Iterating in Python Last Updated : 28 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Modifying a list while iterating can be tricky but there are several ways to do it safely. One simple way to modify a list while iterating is by using a for loop with the index of each item. This allows us to change specific items at certain positions. Python a = [1, 2, 3, 4] for i in range(len(a)): if a[i] % 2 == 0: a[i] = a[i] * 2 print(a) Output[1, 4, 3, 8] Let's look into other methods to Modify a List While Iterating in Python are:Table of ContentUsing List ComprehensionUsing a While LoopUsing a Reverse LoopUsing List ComprehensionCreate a new list based on the modifications and replace the original list if necessary. This avoids directly modifying the list during iteration. Python a = [1, 2, 3, 4] a = [x * 2 if x % 2 == 0 else x for x in a] print(a) Output[1, 4, 3, 8] Using While LoopIn some cases, we might want more control over the iteration process. Using a while loop allows us to modify the list as we go, but we need to be careful to avoid skipping elements. Python a = [1, 2, 3, 4] i = 0 while i < len(a): if a[i] % 2 == 0: a[i] = a[i] * 2 i += 1 print(a) Output[1, 4, 3, 8] Iterating Over a CopyIterate over a copy of the list (list.copy()) to avoid modifying the list during iteration. Python a = [1, 2, 3, 4] for item in a.copy(): if item % 2 != 0: a.remove(item) print(a) Output[2, 4] Comment More infoAdvertise with us Next Article Add items to List While Iterating - Python P pragya22r4 Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Add items to List While Iterating - Python Python has many ways to append to a list while iterating. List comprehension is a compact and efficient way to append elements while iterating. We can use it to build a new list from an existing one or generate new values based on a condition.Pythona = [1, 2, 3] a += [i + 4 for i in range(3)] print( 2 min read Remove Items from a List While Iterating - Python When we need to remove items from a list while iterating, we have several options. If we need to remove specific values, using a for loop with remove() can work, but itâs important to avoid skipping items by iterating over a copy. A while loop offers more control and filter() is a good approach for 2 min read How to iterate through a nested List in Python? A nested list is a list that contains other lists. Working with nested lists can seem tricky at first but it becomes easy once we understand how to iterate through them. This is the easiest way to loop through a nested list. We can use a for loop to access each sublist in the main list, and then use 3 min read Python | Modifying tuple contents with list In Python, tuples are immutable and hence no changes are required in them once they are formed. This restriction makes their processing harder and hence certain operations on tuples are quite useful to have knowledge of. This article deals with modifying the second tuple element with the list given. 6 min read How to Append Multiple Items to a List in Python Appending multiple items to a list in Python can be achieved using several methods, depending on whether you want to extend the list with individual elements or nested collections. Letâs explore the various approaches.Using extend method (Adding multiple items from iterable)The list.extend() method 2 min read How to Append Objects in a List in Python The simplest way to append an object in a list using append() method. This method adds an object to the end of the list. Pythona = [1, 2, 3] #Using append method to add object at the end a.append(4) print(a) Output[1, 2, 3, 4] Let's look into various other methods to append object in any list are:Ta 2 min read Like