Introduction to Dictionaries
Introduction to Dictionaries
Representation
In different programming languages and data structures, key-value pairs can be represented in
various ways:
1.
# "name", "age", and "city" are keys, while "Alice", 30, and "New York" are their values.
-------|---------|-------|-------
"name" | 214 |5 | "Alice"
"age" | 123 |8 | 30
Visual Representation
"name": "Alice",
"age": 30,
Key Features
1. Key-Value Pair:
2. Uniqueness of Keys:
Keys in a dictionary are unique; no two entries can have the same key.
This uniqueness allows for efficient access to values.
3. Fast Lookup:
4. Dynamic Size:
Operations
Example
---python
# Creating a dictionary
student_grades = {
"Alice": 85,
"Bob": 90,
"Charlie": 78
student_grades["David"] = 92
# Updating an entry
student_grades["Alice"] = 88
# Looking up a value
print(student_grades["Bob"]) # Output: 90
# Deleting an entry
del student_grades["Charlie"]
print(f"{student}: {grade}")
Key
Value.
Each item stored in a dictionary is represented by a key-value pair. Key is used to
access the elements in the dictionary. With the key we can access value which has
more information about the element.
Characteristics of Dictionary
Key-Value Pairs: Dictionaries store data as key-value pairs where each key is unique
and maps to exactly one value.
Direct Access: The primary feature of dictionaries is to provide fast access to elements
not by their position, as in lists or arrays, but by their keys.
Dynamic Size: Like many abstract data types, dictionaries typically allow for dynamic
resizing. New key-value pairs can be added, and existing ones can be removed.
Ordering: Some dictionaries maintain the order of elements, such as ordered maps or
sorted dictionaries. Others, like hash tables, do not maintain any particular order.
Key Uniqueness: Each key in a dictionary must be unique, though different keys can
map to the same value.
Types of Dictionary
There are two major variations of dictionaries:
Ordered dictionary.
Unordered dictionary.
Implementation of Dictionary
A dictionary can be implemented in several ways, such as
dictionary = []
Step 2: Insertion
To insert a new key-value pair into the dictionary:
Check for the Key's Existence: Iterate through the list to check if the given key
already exists.
Update or Insert:
Step 3: Search
To find a value associated with a given key:
Iterate Through the List: Go through each key-value pair in the list.
Compare Keys: Check if the current key matches the search key.
Return Value if Found: If a match is found, return the associated value.
function search(key):
for each pair in dictionary:
if pair.key == key:
return pair.value
return None // Or indicate that the key was not found
Step 4: Deletion
To delete a key-value pair from the dictionary:
Search for the Key: Iterate through the list to find the key.
Remove the Pair: Once the key is found, remove the key-value pair from the list.
function delete(key):
for each pair in dictionary:
if pair.key == key:
remove pair from dictionary
return
Step 5: Traversal
To traverse the dictionary and access each key-value pair:
Iterate Through the List: Go through each key-value pair in the list.
Process Each Pair: Perform the desired operation (like printing) on each pair.
function traverse():
for each pair in dictionary:
process(pair.key, pair.value)
Step 6: Update
Updating a value for a specific key follows the same procedure as insertion. If the key
exists, its value is updated; otherwise, the key-value pair is added.
Search Time: The search time is linear in the worst case, which can be inefficient for
large numbers of entries.
Insertion Time: Similar to search, if the list is being kept in sorted order, then finding the
correct place to insert a new key can take linear time.
Deletion Time: Removing an entry requires a search followed by a deletion, both of
which can be inefficient in a large list.
Skip list representation
A Skip List is an advanced data structure that allows for fast search within an ordered sequence
of elements, providing a clever alternative to balanced trees. It is effectively a linked-list that
adds multiple layers on top of the standard list to enable faster search times.
Here's how a Skip List is typically represented:
Base Layer:
The bottom layer is a standard linear linked list that contains all the elements in
the set, sorted by key.
Express Layers:
Above the base layer are one or more layers of "express lanes." Each layer is a
linked list that skips over some elements from the list below it. The topmost layer
has the fewest elements, and each layer doubles (or varies by some factor) the
number of elements it skips compared to the layer directly beneath it.
A Skip List is an efficient, probabilistic data structure that enables fast search, insertion, and
deletion operations, akin to balanced trees like AVL or Red-Black trees. Here's a step-by-step
explanation of how a Skip List is used to represent a dictionary:
Step 3: Initialization
When initializing a Skip List:
Create a "head" node with pointers set to NULL for each layer.
Set a maximum level for the list, which determines how many layers the list can have.
Optionally, set a "tail" node with maximum possible key value to mark the end of each
level.
Step 4: Insertion
To insert a key-value pair:
Find the Position: Start from the topmost layer and move forward until you find a node
with a greater key or reach the end of the layer. Then, move down one layer and continue.
Record the nodes where you move down a level.
Choose a Random Level: For the new node, randomly decide the number of layers
(level) it will participate in (usually done with a coin flip algorithm).
Rearrange Pointers: For each level up to the chosen level, update the pointers of the
recorded nodes to include the new node.
Step 5: Search
To search for a key:
Step 6: Deletion
To delete a key-value pair:
Perform a search for the key, keeping track of the predecessors at each level.
If the key is found, update the pointers of these predecessor nodes to skip the node being
deleted.
Remove the node and deallocate its memory.
Step 8: Traversal
To traverse the Skip List, simply follow the bottom layer from the head node to the end,
processing or printing the values.