0% found this document useful (0 votes)
3 views

Tutorial 12.2 - Tutorial on pseudocode for lists and dictionaries

The document is a tutorial on pseudocode for lists and dictionaries, covering their definitions, operations, and functions. It explains how to manipulate lists using the ++ operator and provides an overview of insertion sort. Additionally, it discusses dictionaries, their key-value structure, and functions for accessing keys and checking for key existence.

Uploaded by

Meera Shridhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Tutorial 12.2 - Tutorial on pseudocode for lists and dictionaries

The document is a tutorial on pseudocode for lists and dictionaries, covering their definitions, operations, and functions. It explains how to manipulate lists using the ++ operator and provides an overview of insertion sort. Additionally, it discusses dictionaries, their key-value structure, and functions for accessing keys and checking for key existence.

Uploaded by

Meera Shridhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Prof. Madhavan Mukund Prof. G.

Venkatesh
Department of Computer Science Indian Institute of Technology Madras
Chennai Mathematical Institute

Mr. Omkar Joshi


Course Instructor
IITM Online Degree Programme
Content
▪ List
• Introduction
• ++ operator
• List functions
▪ Insertion sort
▪ Dictionary
• Introduction
• Dictionary functions
▪ Index vs. Key

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 1


List introduction
▪ List is a collection of values stored in a sequential order.
▪ List can store values of different datatypes.
▪ List can store duplicate values.
▪ List preserves the order of elements in it.
▪ An element in a list can be another list or a dictionary.
▪ List is initialised using [] notation.
▪ l = [] or length(l) == 0, denotes that the list l is empty.

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 2


++ operator
▪ list = list ++ list (it requires list operands)
▪ ++ operator is used for two different operations, append and extend
• Append: It merges two lists
e.g. if l1 = [1, 2, 3] and l2 = [“a”, “b”, “c”] then l1 ++ l2 == [1, 2, 3, “a”, “b”, “c”]

• Extend: It adds an element to a list


e.g. if l = [1, 2, 3] and x = 4 then l ++ [x] == [1, 2, 3, 4]
if l = [1, 2, 3] and x = 4 then [x] ++ l == [4, 1, 2, 3]
l ++ x or x ++ l are incorrect statements because x is an integer variable not a list

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 3


++ operator (continue…)
▪Adding lists to list
e.g. if l = [], l1 = [1, 2, 3] and l2 = [a, b, c]
l = l ++ [l1] then l == [[1, 2, 3]]
l = l ++ [l2] then l == [[1, 2, 3], [a, b, c]]
▪ Adding dictionaries to list
e.g. if l = [], d1 = {1: 1, 2: 4, 3: 9} and d2 = {“a”: “A”, “b”: “B”, “c”: “C”}
l = l ++ [d1] then l == [{1: 1, 2: 4, 3: 9}]
l = l ++ [d2] then l == [{1: 1, 2: 4, 3: 9}, {“a”: “A”, “b”: “B”, “c”: “C”}]

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 4


Input Output / Return type
List (l) length(l) first(l) last(l) rest(l) init(l)
0 [] []
[] Undefined Undefined
Integer List List
1 10 10 [] []
[10]
Integer integer integer List List

4 1 4 [2, 3, 4] [1, 2, 3]
[1, 2, 3, 4]
integer integer integer list list

3 "a" "c" ["b", "c"] ["a", "b"]


["a", "b", "c"]
integer string string list list

6 2 “a" ["b", 3, 1, "c", "a"] [2, "b", 3, 1, "c"]


[2, "b", 3, 1, "c", "a"]
integer integer string list list

3 [1, 2, 3] [1, 8, 27] [[1, 4, 9], [1, 8, 27]] [[1, 2, 3], [1, 4, 9]]
[[1, 2, 3], [1, 4, 9], [1, 8, 27]]
integer list list list list

{"a": "A", "b": "B", "c": [{"a": "A", "b": "B", "c":
[{1: 1, 2: 4, 3: 9}, 2 {1: 1, 2: 4, 3: 9} [{1: 1, 2: 4, 3: 9}]
"C"} "C"}]
{"a": "A", "b": "B", "c": "C"}] integer dictionary list
dictionary list

2 [1, 8, 27] {1: 1, 2: 4, 3: 9} [{1: 1, 2: 4, 3: 9}] [[1, 8, 27]]


[[1, 8, 27], {1: 1, 2: 4, 3: 9}]
integer list dictionary list list

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 5


Insertion sort
inputList = [5, 2, 1, 3, 4]
sortedList = []
foreach x in inputList {
sortedList = insertAnElement (sortedList, x)
}
Procedure insertAnElement (sl, x)



End insertAnElement

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 6


Procedure sortedList /
Procedure insertAnElement (sl, x) call sl
x tempList inserted ele Remark
tempList = [] foreach loop will not
1 [] 5 [] False --
inserted = False execute because sl is empty
[5] Because inserted == False
foreach ele in sl { 2 [5] 2 [] False 5
if (not(inserted)){ [2] True Because x < ele
if (x < ele) { [2, 5]
3 [2, 5] 1 [] False 2 Because x < ele
tempList = tempList ++ [x] [1] True
inserted = True [1, 2]
[1, 2, 5] 5
} 4 [1, 2, 5] 3 [] False 1
} [1] 2
tempList = tempList ++ [ele] [1, 2] 5
[1, 2, 3] True Because x < ele
} [1, 2, 3, 5]
if (not(inserted)) { 5 [1, 2, 3, 5] 4 [] False 1
tempList = tempList ++ [x] [1] 2
[1, 2] 3
} [1, 2, 3] 5
return (tempList) [1, 2, 3, 4] True Because x < ele
[1, 2, 3, 4, 5]
End insertAnElement
[1, 2, 3, 4, 5]

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 7


Dictionary introduction
▪ Dictionary is a collection of elements stored as key: value pair.
▪ Dictionary can store keys and values of different datatypes.
▪ Dictionary can store duplicate values but keys must be unique.
▪ Dictionary does not preserve the order of elements in it.
▪ A value in a dictionary can be another dictionary or a list.
▪ Dictionary is initialised using {} notation.
▪ Dictionary value is accessed using d[key] notation

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 8


Dictionary functions
▪ keys(d) function returns a list of keys present in the dictionary.
e.g. if d = {“a”: 1, “b”: 2, “c”: 3} then keys(d) can return
[“a”, “b”, “c”] or [“a”, “c”, “b”] or [“b”, “c”, “a”] or [“b”, “a”, “c”] or
[“c”, “a”, “b”] or [“c”, “b”, “a”]
Dictionary does not preserve order. Therefore, keys function can return the list of keys in
any order.

▪ isKey(d, k) function returns True if k is a key in dictionary d else it returns


False
e.g. if d = {“a”: 1, “b”: 2, “c”: 3} then isKey(d, “a”) will return True
e.g. if d = {“a”: 1, “b”: 2, “c”: 3} then isKey(d, “z”) will return False

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 9


Index vs. Key
Index Key
List has indices Dictionary has keys
Index indicates the position of the element in Key is an unique entity used to identify its
the list value in the dictionary
Index is always an integer starting from 0 to n Key can be of any datatype
e.g. l = [10, 20, 30, 40] then index 0 is 10, e.g. d = {1: 10, 2: 20, 3: 30, 4: 40} then d[2]
index 1 is 20 and so on. notation is used to access the value 20

TUTORIAL ON PSEUDOCODE FOR LISTS AND DICTIONARIES 10

You might also like