Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Python List Manipulation

Append the number 9 to the list

Python
my_list = [1, 2, 3, 4, 5]

To append the number 9 to the list my_list, you can use the append() method.

Here’s how you can do it:

Python
my_list.append(9)

After this operation, my_list will be [1, 2, 3, 4, 5, 9] with 9 added as the last element.


Remove the element 4 from the list

To remove the element 4 from the list my_list, you can use the remove() method.

Here’s how you can do it:

Python
my_list.remove(4)

After this operation, my_list will be [1, 2, 3, 5, 6] with 4 removed from the list.


Replace the second element with 11.

To replace the second element (index 1) with the value 11 in the list my_list, you can simply assign the new value to that index.

Here’s how you can do it:

Python
my_list[1] = 11

After this operation, my_list will be [1, 11, 3, 5, 6] with the second element replaced by 11.