https://github.com/dyanni3/invertibledict
python dictionary with easy conversion of keys to values and vice versa
https://github.com/dyanni3/invertibledict
Last synced: 3 months ago
JSON representation
python dictionary with easy conversion of keys to values and vice versa
- Host: GitHub
- URL: https://github.com/dyanni3/invertibledict
- Owner: dyanni3
- License: mit
- Created: 2021-11-18T17:37:25.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-18T21:37:11.000Z (over 3 years ago)
- Last Synced: 2025-01-11T06:13:11.735Z (5 months ago)
- Language: Python
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# InvertibleDict
python dictionary with easy conversion of keys to values and vice versa# Examples
1. Inherits from python dict, so use it like a normal dictionary
```
inv_dict = InvertibleDict({1:1, 2:2, 3:5})
for k, v in inv_dict.items():
...
inv_dict.update({4:9})
```2. Swap the keys and values easily
```
d = InvertibleDict({1:[1,2,3], 2:[4,5,6]})
print(d, ~d)
````{1: (1, 2, 3), 2: (4, 5, 6)} {1: 1, 2: 1, 3: 1, 4: 2, 5: 2, 6: 2}`
```
swapped = ~inv_dict
original = ~(~inv_dict)
assert original == inv_dict
```3. Handles strings and other iterable keys and values appropriately
```
d = InvertibleDict({'abc':1, 'def':2})
e = ~d
print(d, e, ~e == d)
````>>> {'abc': 1, 'def': 2} {1: 'abc', 2: 'def'} True`