-->
How do you Update a List in Python using a For Loop With Examples?

How do you Update a List in Python using a For Loop With Examples?

Back to top

Updated by Ashirafu Kibalama on September 17, 2024

"How do you update a list in Python using a for loop?"





To update a list in Python using a for loop, you can iterate over the list's indices and modify each element in place.


You can use a for loop in Python to update various tasks, such as multiplying numbers, changing strings to uppercase, or applying your custom functions to each item in the list.


4 Examples To Update a List in Python Using a For Loop


Example 1) Using a for loop to update each element in the lists by reversing each sublist:


item1 = [1, 2, 3]
item2 = [4, 5, 6]
item3 = [7, 8, 9]

# Create a list of these items
my_list = [item1, item2, item3]

# Update each sublist by reversing it
for i in range(len(my_list)):
my_list[i] = my_list[i][::-1]

print(my_list)



  • my_list[i][::-1] reverses the sublist at index i.
  • The loop iterates over each sublist in my_list and updates it with the reversed version.


Output:





Example 2) Using a for loop to update each element in the list by Incrementing each element by 1:



numbers = [1, 2, 3, 4, 5]

# Update each element by incrementing it by 1
for i in range(len(numbers)):
numbers[i] += 1

print(numbers)


  • range(len(numbers)) generates indices from 0 to len(numbers) - 1.
  • numbers[i] += 1 updates each element at index i by adding 1.


Output:





Example 3) Using a for loop to update each element in the list by Converting each string in a list to uppercase:



words = ["apple", "banana", "cherry"]

# Update each element by converting it to uppercase
for i in range(len(words)):
words[i] = words[i].upper()

print(words)


  • words[i] = words[i].upper() updates each string in the list to its uppercase form.


Output:





Example 4) Using a for loop to update each element in the list by Multiplying each element by 2:



numbers = [1, 2, 3, 4, 5]

# Update each element by multiplying it by 2
for i in range(len(numbers)):
numbers[i] *= 2

print(numbers)


  • numbers[i] *= 2 update each element by multiplying it by 2.


Output:





Conclusion

In each example above, the for loop iterates over the list and updates its elements in place.


To update a list in Python with a for loop allows you to perform various operations like:

  • arithmetic updates,
  • string transformations,
  • or applying custom functions to each element in the list.


Keep experimenting with different operations, and you'll see how helpful Python's for loop can be in your daily coding projects.


Related Posts:

1  How Does List Comprehension Work in Python?


2  How do we Compare Two Lists Using List Comprehension Python?


3 With an Example Fix: attributeerror: 'list' object has no attribute 'update_relative' Python


Other Posts:

1  Fixing attributeerror: 'freetypefont' object has no attribute 'getsize' and AttributeError: 'FreeTypeFont object has no attribute 'read Python


2 How do you Fix or Solve an Attribute Error if the 'str' Object has no Attribute in Python?


3 With an Example Fix: Os error Cannot Open Resource Python / OSError: Cannot Open Resource imagefont


4 With an Example Fix: AttributeError: 'ImageDraw' object has no attribute 'textsize' Python