Updating Array Elements
In the previous exercise, we learned how to access array elements using their indices. Now let's look at how we can update the values of these elements.
Updating an array element is simple. We use the same syntax we used to access the elements, and assign a new value using the =
operator just as we would assign a value to a variable.
Let's look at an example:
newYearResolutions = ["Keep a journal", "Attend a falconry course", "Learn to juggle"]
Imagine we want to change the second resolution. We use the following code, where we place a new element at index 1:
newYearResolutions[1] = "Attend a painting course"
Now the array newYearResolutions
will look like this:
['Keep a journal', 'Attend a painting course', 'Learn to juggle']
Instructions
Change the value of the second colleague to "Daniel"
.
Print the value of the variable colleagues
.
Start programming for free
4/10