Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Replacing Elements in a List

00:00 One of the reasons lists are so versatile is that Python provides many ways to mutate or change them. In this lesson, you’ll look at using assignment to update lists. Like you saw in the previous lesson, you can replace a single element using index assignment.

00:14 The syntax for that would be my_list [index] = new_value. Very straightforward. What’s much more powerful is replacing multiple elements using slice assignment.

00:24 The syntax is similar to accessing a slice. my_list at slice start, :stop:step = iterable. When using slice assignment, it’s super important to note that it can effectively add or remove elements, since the length of the iterable on the right hand side of the assignment doesn’t have to match the size of the selected slice.

00:44 Curious to see this in action? Meet me in the REPL. To explore some index assignments, create a list of integers one to seven and call it numbers.

00:59 Now, change the second element to the string "second" using assignment. numbers at index one equals "second".

01:07 Confirm that numbers has updated and the integer two has been replaced with the string "second". Naturally, this works with reverse indexing as well.

01:16 Change the last value in numbers to the string "last", numbers at index negative one equals "last".

01:25 Look at numbers and the final element is now the string "last". Enough of that. Let’s finally check out slice assignments. Remember, on the left hand side of the assignment, you’ll select a slice of the list, and on the right hand side you’ll supply an iterable.

01:41 Replace the second, third, and fourth elements of numbers with the integers, 22, 33, and 44. numbers sliced from one to four equals the list [22, 33, 44]. Check numbers and the elements have been replaced.

01:59 In this case, the list stayed the same size, seven elements, because slicing from one to four selects three elements and the list you used as replacement had three elements as well, but they need not match.

02:11 Try this: numbers sliced from six onwards equals the list [7, 8, 9], and look at numbers. Because your slice starts at index six, which was previously the last element of the list, it’s replaced by the first element of the supplemental list.

02:28 Then the following elements are added as well, taking the total number of elements from seven to nine.

02:34 And it works the other way as well. Try setting the middle of the list to an empty list. numbers sliced from one to eight equals an empty list,

02:44 and the result is just the first and last elements of numbers, one and nine. The slice you selected has been replaced by nothing,

02:53 and the iterable you use on the right hand side of the assignment doesn’t have to be a list either. Try this: numbers sliced from index one to index one equals range(2, 9).

03:06 By selecting a slice of one to one, which is actually a slice of zero length, you’re inserting the values from the iterable into the list at that index. By using a range from two to nine, you’re providing the integers two through eight as a replacement, and your result numbers now holds the integers one through nine.

03:25 Pretty neat, huh? Like other techniques you’ve seen already, you can do a lot with slice assignments as long as you understand the nuances of their behavior, making them a powerful tool.

03:37 Coming up next, you’ll look at some more direct ways to add and remove elements from lists.

Become a Member to join the conversation.