Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python List Items Modifying and Updating tutorial, we'll cover different ways to modify and update elements in a list. We will focus on the following methods:
enumerate()
functionmap()
functionYou can access and modify list items directly using their indices:
my_list = [1, 2, 3, 4, 5] # Update the second item (index 1) my_list[1] = 20 print(my_list) # Output: [1, 20, 3, 4, 5]
enumerate()
functionThe enumerate()
function allows you to iterate through a list while also keeping track of the index of the current item. You can use this index to update the corresponding item in the list:
my_list = [1, 2, 3, 4, 5] for index, item in enumerate(my_list): my_list[index] = item * 2 print(my_list) # Output: [2, 4, 6, 8, 10]
List comprehension is a concise way to create a new list by applying an expression to each element in an existing list:
my_list = [1, 2, 3, 4, 5] doubled_list = [x * 2 for x in my_list] print(doubled_list) # Output: [2, 4, 6, 8, 10]
Keep in mind that list comprehension creates a new list, so the original list remains unchanged.
map()
functionThe map()
function applies a specified function to each item in an iterable (e.g., list) and returns a map object. You can use this to update elements in a list:
my_list = [1, 2, 3, 4, 5] def double(x): return x * 2 doubled_list = map(double, my_list) print(list(doubled_list)) # Output: [2, 4, 6, 8, 10]
Similar to list comprehension, the map()
function creates a new list, so the original list remains unchanged.
In this tutorial, you learned different ways to modify and update elements in a list in Python, including using indices, the enumerate()
function, list comprehension, and the map()
function.
Updating elements of a list in Python:
my_list = [1, 2, 3, 4, 5] my_list[2] = 99 print(my_list) # Output: [1, 2, 99, 4, 5]
Change values in a Python list:
my_list = ['apple', 'orange', 'banana'] my_list[1] = 'grape' print(my_list) # Output: ['apple', 'grape', 'banana']
Modifying list items using a for loop in Python:
for
loop can be used to iterate through a list and modify its elements.my_list = [6, 7, 8, 9, 10] for i in range(len(my_list)): my_list[i] += 1 print(my_list) # Output: [7, 8, 9, 10, 11]
In-place modification of list elements in Python:
my_list = [12, 13, 14, 15, 16] my_list[:] = [x * 2 for x in my_list] print(my_list) # Output: [24, 26, 28, 30, 32]
How to update multiple list items in Python:
my_list = [17, 18, 19, 20, 21] indices_to_update = [1, 3] new_values = [99, 100] for i, value in zip(indices_to_update, new_values): my_list[i] = value print(my_list) # Output: [17, 99, 19, 100, 21]
Using list comprehension to modify list elements in Python:
my_list = [22, 23, 24, 25, 26] modified_list = [x + 5 for x in my_list] print(modified_list)
Appending and extending lists in Python:
my_list = [27, 28, 29] my_list.append(30) print(my_list) # Output: [27, 28, 29, 30] additional_elements = [31, 32, 33] my_list.extend(additional_elements) print(my_list) # Output: [27, 28, 29, 30, 31, 32, 33]
Updating specific indices in a list in Python:
my_list = ['cat', 'dog', 'rabbit', 'turtle'] indices_to_update = [0, 2] new_values = ['kitten', 'bunny'] for i, value in zip(indices_to_update, new_values): my_list[i] = value print(my_list) # Output: ['kitten', 'dog', 'bunny', 'turtle']