VJC Chapter 4 Data Structures
Uploaded by cheesemuffin · 10 December 2025
Preview
Text from the first pagesVJC/H2Computing/9569 Chapter 4: Data Structures Contents 1 Lists 1.1 Create 1.2 Add list items 1.2.1 Append 1.2.2 Insert 1.2.3 Extend 1.2.4 Using operator 1.3 Read 1.4 Update 1.5 Delete 1.5.1 Remove item by value 1.5.2 Remove item by index 1.5.3 Del keyword 1.5.4 Clear a list 1.6 Working with List 1.6.1 Min, Max and Sum 1.6.2 Reversing 1.6.3 Sorting 1.6.4 Membership and searching 1.7 Copying List 1.7.1 By Slicing 1.7.2 By Constructor 1.7.3 The copy() method 1.8 List Comprehension 2 Tuples 2.1 Unpacking Tuples 3 Dictionaries 3.1 Create 3.2 Access 3.3 Update 3.4 Add 3.5 Delete Syllabus Learning Outcomes 2.2 Programming Elements and Constructs Use programming language elements and constructs to write recursive and non-recursive programs to solve a variety of problems. 2.2.1 Understand the different types: integer, real, char, string and Boolean and initialise arrays (1-dimensional and 2-dimensional). 1
VJC/H2Computing/9569 1 Lists Lists are the most commonly used data structure in Python. Lists are used to store multiple items in a single variable. List items are ordered, mutable and allow duplicate values. A list collection is mutable means its items can be added, updated and removed. Each of these data can be accessed by calling its index value. 1.1 Create Lists are created using square brackets. There are a few ways to initialise a list. List items can be of any data type. A list can also contain different data types. 2
VJC/H2Computing/9569 A list can also contain sublists, which is known as a nested list. 1.2 Add list items 1.2.1 Append The append( ) method is used to append an element at the end of the list. 1.2.2 Insert The insert() method inserts an item at the specified index. 1.2.3 Extend A list can also be extended with items from another list using extend() method. It will modify the first list. The resultant list will contain all the elements of the lists that were added, i.e. the resultant list is NOT a nested list. 3
VJC/H2Computing/9569 1.2.4 Using operator Two lists can also be join together simply using + operator. Similar to String, we can repeat a list multiple times with * operator. 1.3 Access list items List items are indexed and you can access them by referring to the index number. The first item has index 0. We can also access the elements in the list using negative indexing. That is the last element has an index of -1, and second last element has index of -2 and so on. To iterate through a list, we can use for loop. 4
VJC/H2Computing/9569 Similar to Strings, we can use the len() method to get the number of elements in a given list. Indexing was only limited to accessing a single element. Slicing on the other hand is accessing a sequence of data inside the list. Slicing is done by defining the index values of the first element and the last element from the parent list that is required in the sliced list, similar to Strings. sub = num[a : b] sub = num[a : ] sub = num[: b] sub = num[:] if both a and b are specified, a is the first index, b - 1 is the last index. Note that the upper bound index is NOT inclusive. if b is omitted, it will slice till last element. if a is omitted, it will starts from first element. if neither a or b is specified, it is effectively copy the whole list 5
VJC/H2Computing/9569 The index() method returns the position at the first occurrence of the specified value. If there are multiple items of the same value, only the first index value of that item is returned. You can add 2nd argument to start searching from a particular index onwards. 1.4 Update An item in the list can be updated by its index value. You can update more than one item by specifying a range of index numbers where you want to insert the new items. If you insert more items than you replace/update, the new items will be inserted where you specified, and the remaining items will move accordingly. 6
VJC/H2Computing/9569 1.5 Delete 1.5.1 Remove item by value The remove( ) method remove an item based on its value. If there are multiple items of same value, it will only remove 1st item. It will throw an exception if the value is not found in the list. 1.5.2 Remove item by index The pop( ) method removes the element at the specified position. If no position is specified, last item is removed by default. The pop() method returns removed value. 7
VJC/H2Computing/9569 1.5.3 Del keyword The del keyword is used to delete objects. In Python, everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc. del num deleted the num list entirely, hence an error is encountered when the print(num) command is executed. 1.5.4 Clear a list To clear all elements in a list, use its clear() method. 1.6 Working with List 1.6.1 Min, Max and Sum If the list consists of all integer elements, the min( ), max( ) and sum() gives the minimum item, maximum item and total sum value of the list. 8
VJC/H2Computing/9569 For a list with elements as string, the max( ) and min( ) is still applicable. The max( ) method would return a string element whose ASCII value is the highest. The min( ) method is used to return the lowest. Take note that only the first index of each element is considered each time and if they value is the same then second index considered so on and so forth. 1.6.2 Reversing The entire elements present in the list can be reversed by using the reverse() method. 1.6.3 Sorting List has a built in method sort( ) to arrange the elements in ascending order by default. For descending order, specify the named argument reverse = True. Note that both the sort() and reverse() methods mentioned above will alter the original sequence of items in the list. The sorted() method sorts the given sequence either in ascending order or in descending order and returns the sorted list. This method does not effect the original sequence of items in the list. 9
VJC/H2Computing/9569 1.6.4 Membership and searching You can use the in (not in) statement for membership of an item in a list. The count() method can count the occurrence of a particular item in a list. 1.7 Copying List You cannot copy a list simply by typing list2 = list1, because list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2. 10
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

