Set, Dictionary, JSON

HSS 611: Programming for HSS

Taegyoon Kim

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)

  • Looks through all items one by one from the start

    • ‘apple’ is not ‘cherry’
    • ‘orange’ is not ‘cherry’
    • ‘cherry’ is ‘cherry’, stop, return True
  • Could have gone until end of the list.

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(n2) (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

  • What will it return?

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

Methods

  • intersection() and intersection_update() work similarly

  • Check out this link for more

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

  • Another example

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

  • E.g., salary['Jane']

Dictionary

But dictionaries are not quite indexable/sliceable

  • A way to get around this (rely on .items())

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)

Person Grade
Ali A+
Bella A+
Rose A
Sam B+

Dictionary

An example

  • What’s Rose’s grade?

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

  • Let’s create one

Dictionary

Methods

  • Get all keys in the dictionary

Dictionary

Methods

  • Get all the values

Dictionary

Methods

  • Get all the pairs

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

Operations

  • Remove item from dictionary

  • Check grades

  • Alternatively

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

  • Let’s try it

Dictionary

Nested dictionaries

  • E.g., science fictions

Dictionary

Nested dictionaries

  • E.g., science fictions

Dictionary

Nested dictionaries

  • E.g., science fictions

    • Use PrettyPrinter function in pprint module
    • For more on pprint see here

Dictionary

Nested dictionaries

  • Indexing/slicing

Dictionary

Dictionary methods

  • Check out the full list of dictionary methods here

Dictionary

Comprehension

  • Dictionary comprehension: the same thing, except the first part is a key:value pair

Dictionary

Comprehension

  • Combine two lists into a dictionary without dictionary comprehension

Dictionary

Comprehension

  • Another way, but still without dictionary comprehension

  • Use zip() function

Dictionary

Comprehension

  • With dictionary comprehension

Dictionary

Comprehension

  • We can add conditions with if-else block

  • Condition on key:

  • Condition on value:

Dictionary

Comprehension

  • What does it do?

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

    • Dictionaries
    • Lists

JSON

Python has built-in json module

  • json.loads() creates a dictionary or list by from a string

JSON

Python has built-in json module

  • Python reads this in as a dictionary/list

JSON

Python has built-in json module

  • Again, json.loads() creates a dict or a list from a string (not from a file)
  • From a string (thus the s)

JSON

Python has built-in json module

  • json.loads() creates a dict or a list from a string (not from a file)
  • From a string (thus the s)

JSON

Whether JSON will be read as dict or list depends on content

  • If it is structured in {}, it’ll be a dictionary

  • If it is structured in [], it’ll be a list

  • Of course, it can be any combination of things:

    • A dictionary where values are a list
    • A list of dictionaries
    • Any different combinations or nesting of these
    • This is what makes it very flexible

JSON

json.dumps() turns a dictionary/list as a string (thus the s)

JSON

json.dump() writes a dictionary/list to a JSON file

JSON

json.load() reads a JSON file as a dictionary/list

JSON

Dumps and loads

  • json.dumps(): turn a Python object into a JSON string

  • json.loads(): turn a JSON string back into a Python object

JSON

Dump and load

  • json.dump(): turn a Python object and write JSON into a file

  • json.load(): turn a JSON string back into a Python object