Exercise: List mutation
In these exercises, you'll practice list mutation using both list methods and bracket notation.
Add to sequence
Implement add_to_sequence
, a function that adds a final item to the end of a list that is 1 more than the previous final item of the list.
If you'd like more guidance, expand this block.
- You should first use bracket notation to access the final item of the list.
- Then add one to the final item.
- Finally, use a list method to add that value to the end of the list.
For a code template, expand this block.
last_num = sequence[__]
sequence.______(last_num + 1)
Malcolm in the Middle
Implement malcolm_in_middle
, a function that adds a new element with the string 'malcolm' in the middle of the provided list. In the case of a list with odd elements, the string should be inserted to the "right" of the middle. See doctests for examples.
If you'd like more guidance, expand this block.
- Your solution can be a single line of code.
- Your code should use a list method to insert the new element.
- Your code needs to calculate the index based on the current length of the list.
For a code template, expand this block.
sequence.________(___________, 'malcolm')
Double time
Implement double_time
, a function that doubles each element in the provided list.
Tip: Remember there are several ways to loops through lists. Which way will work better here?
🧩 For more guidance, try a Parsons Puzzle version first.