Set, Dictionary, JSON
HSS 611: Programming for HSS
Sep 16, 2025
Agenda
- Brief intro to time complexity
- Understand why we need sets and dictionaries
- Set
- Dictionary
- JSON
Time Complexity
What is time complexity?
- Originals from computer science
- As the size of input grows, how does time behave?
- Used to evaluate the efficiency of an algorithm
Time Complexity
Time complexity of operations with different data objects
- Need to understand how objects are represented in memory
- For instance, list:
- Ordered objects are stored one after another
0 -> 1 -> 2 -> 3
(start from 0
, go one by one)
Time Complexity
Check whether an item is in the list (with in
operator)
Time Complexity
By default we think of the worst case scenario
So we look at upper limit
Assume the length of the list is n
In the worst-case scenario, the item being searched for is not present
Time Complexity
Big O notation
- Denoted O( )
- Big O notation describes the behavior of an algorithm as the size of input grows
- O(
n
) means when input size doubles, triples, etc., time will grow linearly too (linear time)
- O(1) (constant time), O(
n
2) (quadratic time), O(2n
) (exponential time), etc.
- Then, what is the time complexity of finding an item in a list?
Set
Properties of sets
- Sets store unique elements—no duplicates
- Sets do not order items
- Use hashing to efficiently store and retrieve elements
- Hashing, through a hash function, maps elements to positions in memory
- See here for more on hashing
- Great for look-ups and other operations (e.g., deletion)
Set
Checking whether an item is in a set
- Always takes the same time
- Does not depend on set size
- O(
1
) (constant time in Big O notation)
Set
Time spent searching for 10n+1 in a list/set whose length is 10n
![]()
Set
Creating a set
- Created with curly braces
- Empty set is created with
set()
Set
Let’s see how an item is searched from a set
- E.g., create a set of individuals banned from flight
Set
Say John wants to buy a ticket
- We can actually check the hash values
Set
Set
- Will only check the relevant memory location
List
- It will check the list one by one, till the end if necessary
Set
Indexing/slicing
- The order in which we supply the items does not matter for internal representation
Set
Methods
- To add an element to a set,
.add()
an element to a set (without re-assignment)
Set
Methods
- If we add again, it will not change anything (duplicates not allowed)
Set
Methods
union()
will return the union of the two sets
- But does not update the original set
Set
Methods
- The
update()
method would both take the union and update original
Set
Methods
difference()
will return the difference (no update though)
- Original set still includes ‘Ali’
Set
Methods
difference_update()
will actually modify the original set
Set
Comprehension
- Similar to list comprehension
- Use curly braces
{}
- Absence of
key:value
will make it a set
- Unique letters in a string
- An example
Set
Comprehension
Dictionary
Dictionaries are made up of key
: value
pairs
- Naturally they are useful for storing associative data, where each key is associated with a specific value
- Created with curly braces:
Dictionary
Initialize an empty dictionary with { }
Alternatively
Dictionary
Access the value using the key
Dictionary
Assign new values to existing key (mutable)
Dictionary
Create new entry
- Check if new entry is there
Dictionary
We could achieve the same functionality with two lists as long as those are stored in correct order (but not quite efficient)
Ali |
A+ |
Bella |
A+ |
Rose |
A |
Sam |
B+ |
Dictionary
An example
Dictionary
Time complexity
This is, again, time inefficient
Takes O(n
) time
Dictionary takes the constant time, O(1
)
Dictionary has unique, unordrered keys (just like set)
Dictionary
Methods
Dictionary
Methods
- Get all keys in the dictionary
Dictionary
Methods
Dictionary
Methods
Dictionary
Iterating over a dictionary
- Will automatically iterate over keys if not specified (e.g., using grades.values())
Dictionary
Iterating over a dictionary
- We can also iterate over pairs
- Or, without the parentheses
Dictionary
Test if key in dictionary with in operator
Dictionary
More on keys and values
- Keys
- Must be unique (like we do not have the same words in actual dictionaries)
- Values
- Can be duplicates (yes, unsurprisingly)
- Can be lists, other dictionaries, etc.
- No order to keys (and thus values), just like there is no order in a set
- Hashing and order are in tension
Dictionary
More on keys and values
- What is this function for? How does it work?
Dictionary
More on keys and values
Dictionary
Dictionary methods
- Check out the full list of dictionary methods here
JSON
JavaScript Object Notation
A file format that stores structured data in a simple and readable way
Very popular for exchanging data with web servers
Let’s see an example of Twitter here
JSON
JSON is built on two main data structures
A collection of key/value pairs ('object'
)
An ordered sequence of items ('array'
)
Makes it a very natural way of storing