Note
Module Scope: This directory (12.Dictionary) contains 20 modernized Python scripts along with an automated unit test suite (test_dict.py). All scripts detail dictionary key-value mapping, ordering, value sorting algorithms, and update patterns across Python 3.3 through Python 3.13.
-
Python Code Modernization (3.3 – 3.13 & 2.7 Comparison):
- Refactored all scripts to PEP 8 standards with modular function structures and testable return values.
- Handled non-interactive input shimming (
try: input = raw_input except NameError: pass). - Corrected spellings (
December,Swedish,Victoria,lambda,sort).
-
Automated Unit Test Suite (test_dict.py):
- Comprehensive test runner using
unittestverifying all 20 scripts. - 100% test pass rate (19/19 test cases passing in 0.001s).
- Comprehensive test runner using
-
Documentation (docs.md):
- Detailed technical breakdown of compact hash tables (PEP 468), PEP 584 dictionary union operators (
|,|=), dynamic view objects (dict_keys,dict_values), and value sorting algorithms.
- Detailed technical breakdown of compact hash tables (PEP 468), PEP 584 dictionary union operators (
Python dictionaries implement key-value mappings powered by CPython compact hash tables.
| Aspect | Python 2.7 Legacy | Python 3.3 – 3.13 Standard | Status in Workspace |
|---|---|---|---|
| Ordering | Unordered (requires OrderedDict) |
Preserves insertion order (Py3.7+) | Native order preservation |
| Views vs Lists | keys() returns list |
keys() returns dynamic view object |
Py3 View objects used |
| Union Operators | Method .update() only |
Union operators | and |= (Py3.9+) |
Documented Py3.9+ operators |
| Test Suite Coverage | Manual execution | Automated unittest runner |
100% Pass Rate (19/19) |
- Key-Value Pairs: Retrieve values by key
dict[key]or safely with default fallbackdict.get(key, default). - Modifying & Deleting: Update entries via
dict[key] = valor remove usingpop(key)ordel dict[key]. - Iterating: Loop over key-value pairs cleanly using
for key, value in my_dict.items():.
- Compact Hash Tables (Py3.6+): Memory footprint reduced by up to 25% by separating indices array from entries array (
indices: array of int,entries: array of (hash, key, value)). - Sorting by Value: Use
operator.itemgetter(1)withsorted(dict.items(), key=...)for high-performance C-level value sorting. - View Objects:
dict.keys(),values(), anditems()are dynamic views that update automatically as the underlying dictionary mutates.
__init__.py: Module initialization metadata.build_dict.py: Manual key assignment and key/value list extraction.copy_to_dic.py: Shallow dictionary copying using.copy().count_words.py: File token frequency counting and sorting using dictionaries.dict_.py: Value sorting usinglambdafunctions.dict_1.py: Basic dictionary creation, item retrieval, and string indexing.dict_list.py: Handling collections of structured dictionary records.dict_num.py: In-place numeric arithmetic operations on dictionary values.dict_num_1.py: Aggregations (sum,max) over dictionary numerical values.dict_update.py: In-place updates via.update()and deletion viadel.dict_update_func.py: Chained dictionary updates and length evaluations.dir_dic.py: Introspecting public dictionary methods viadir().empty_dict.py: Empty dictionary instantiation and dynamic item addition.lib_dict.py: Dictionary class method introspection.my_calender.py: Lookup techniques using.get()with custom defaults.nested_dict.py: Navigating multi-level nested dictionaries.read_dict.py: Key iteration vs.items()tuple pair iteration.remove_from_dic.py: Value extraction and key deletion via.pop().remove_from_dict.py: Safe deletion using.pop()and.popitem().sort_dict_by_value.py: Value sorting comparison:lambdavsoperator.itemgetter.
Run the test suite from the terminal:
python3 -m unittest discover -s ~/PycharmProjects/Devel/Python/12.Dictionary -p "test_*.py"----------------------------------------------------------------------
Ran 19 tests in 0.001s
OK
| Feature / Operation | Python 3.3 Standard | Python 3.13 Standard | Code Context & Performance Impact |
|---|---|---|---|
| Insertion Order Preservation | Unordered dictionary key storage | Guaranteed insertion ordering (PEP 468 / Py3.7+) | Iteration order in dict_list.py, read_dict.py |
| Dictionary Merge/Update Operators | Requires .update() method |
Union operators | and |= (PEP 584 / Py3.9+) |
dict_update.py, dict_update_func.py |
| Memory Architecture | Dense hash array per entry | Compact hash table design (20-25% RAM reduction in Py3.6+) | Large record collections in dict_list.py |
| Structural Pattern Matching | Not supported | Mapping pattern matching match ... case {'key': val} (Py3.10+) |
Deep mapping lookups in nested_dict.py |
| Views vs Lists | dict_keys, dict_values, dict_items views |
Optimized set operations on dict_keys / dict_items |
dir_dic.py, lib_dict.py, read_dict.py |
Warning
1. Unordered Iteration in Legacy Python 2.7
- Python 2.7: Dictionary keys were stored in non-deterministic pseudo-random hash order. Iterating over keys or
.keys()produced unpredictable sequences.collections.OrderedDictwas required for order preservation. - Python 3.3 – 3.13: Python 3.7+ guarantees key iteration order matches item insertion order.
- Script Relevance:
read_dict.py,dict_list.py,build_dict.py.
Warning
2. dict.keys(), values(), items() Return Types
- Python 2.7:
d.keys(),d.values(), andd.items()allocated and returned full standardlistobjects. Modifying the dictionary after callingkeys()did NOT update the returned list. - Python 3.3 – 3.13: Returns dynamic
dict_keys,dict_values, anddict_itemsview objects reflecting mutations to the underlying dictionary in real time. - Script Relevance:
build_dict.py,dir_dic.py,read_dict.py.
Warning
3. dict.has_key(key) Deprecation
- Python 2.7: Supported
d.has_key('name'). - Python 3.3 – 3.13:
has_key()was completely removed. Theinmembership operator ('name' in d) is mandatory. - Script Relevance:
dict_1.py,dict_update.py.
Note
4. Safe Key Lookup with Defaults (.get())
- Python 2.7 & Python 3.3 – 3.13: Directly accessing a missing key
d['missing']raises aKeyError. Usingd.get('missing', default_value)provides a non-crashing safe lookup mechanism. - Script Relevance:
my_calender.py,count_words.py.