10 Prove : Homework - Data Structures
List Manipulation
Outcomes
At the end of this study, successful students will be able to:
Use list manipulation in Python to solve problems.
Overview
One of the convenient features of Python is its ability to work with and manipulate lists. The data structures homework for the next few weeks will highlight different features in this regard. This week, you will learn about slicing and manipulating lists directly.
Preparation Material
Please read the following:
Digital Ocean: How to use list methods - An overview of the basic list methods (append, extend, count, etc.)
Digital Ocean: How to index and slice strings in Python. - This talks about working with strings specifically, but the concepts apply to all lists.
Medium: Lists and List Manipulation - A video that highlights basic operations, include slicing and sorting.
Programming Assignment
Practice using list manipulation and slicing in Python.
Instructions
For each of the following cases, begin with the following list:
numbers = [12, 18, 128, 48, 2348, 21, 18, 3, 2, 42, 96, 11, 42, 12, 18]
Write Python code to accomplish each of the following tasks, then print out the result to verify that your code was correct. Keep in mind it is easy to be off by one when working with indexes/slicing:
Insert the number 5 to the beginning of the list.
Remove the number 2348 based on its value (as opposed to a hard-coded index of 4) from the list.
Create a second list of 5 different numbers and add them to the end of the list in one step. (Make sure it adds the numbers to the list such as: [1, 2, 3, 4, 5] not as a sub list, such as [1, 2, 3, [4, 5]] ).
Sort the list using the built in sorting algorithm.
Sort the list backwards using the built in sorting algorithm.
Use a built-in function to count the number of 12's in the list.
Use a built-in function to find the index of the number 96.
Use slicing to get the first half of the list, then get the second half of the list and make sure that nothing was left out or duplicated in the middle.
Use slicing to create a new list that has every other item from the original list (i.e., skip elements, using the step functionality).
Use slicing to get the last 5 items of the list. For this, please use negative number indexing.
Testing your program
An automated testBed script is not available. Instead, please manually test each task and ensure that it works correctly.
Submission
You are encouraged to work with others on the programming.
When you have completed each of the tasks above, submit the accompanying I-Learn quiz.