Python Language Dictionary: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 37: Line 37:


The keys <code>1</code> and <code>True</code> are equivalent. <font color=darkkhaki>Why?</font>
The keys <code>1</code> and <code>True</code> are equivalent. <font color=darkkhaki>Why?</font>
==<tt>None</tt> as Key==
<code>None</code> is a valid key:
<syntaxhighlight lang='py'>
d = {}
d[None] = 10
d['A'] = 20
assert len(d.keys()) == 2
assert d[None] == 10
assert d['A'] == 20
</syntaxhighlight>


=Dictionaries and the <tt>__hash__()</tt> Function=
=Dictionaries and the <tt>__hash__()</tt> Function=
{{Internal|Eq_()_and_hash_()_in_Python#Dictionaries_and_the_hash_.28.29_Function|Dictionaries and the <code>__hash__()</code> Function}}
{{Internal|Eq_()_and_hash_()_in_Python#Dictionaries_and_the_hash_.28.29_Function|Dictionaries and the <code>__hash__()</code> Function}}
=Dictionaries and the <tt>==</tt> Operator=
<font color=darkkhaki>It would be nice if <code>==</code> recursively deep-compares maps, and it seems to be working, Research this and come up with a definitive conclusion.</font>


=Create a Dictionary=
=Create a Dictionary=
Line 150: Line 166:
print(d.keys())
print(d.keys())
</syntaxhighlight>
</syntaxhighlight>
<font color=darkkhaki>In Python 3, the <code>keys()</code> will return a <code>dict_keys()</code>, which is an iterable view of keys. This is useful with large dictionaries because the runtime does not use the time and the memory to create and store a list that might not be used. In case you need a list, use <code>list()</code> to wrap the result of <code>keys()</code>.</font>
In Python 3, the <code>keys()</code> will return a <code>dict_keys()</code>, which is an iterable view of keys. This is useful with large dictionaries because the runtime does not use the time and the memory to create and store a list that might not be used. In case you need a list, use <code>list()</code> to wrap the result of <code>keys()</code>.
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
list(d.keys())
list(d.keys())
Line 312: Line 328:
</syntaxhighlight>
</syntaxhighlight>
=Recursively Merge Dictionaries=
=Recursively Merge Dictionaries=
<font color=darkkhaki>Requires testing.</font>
This only works on shallow trees, it fails to do deep recursive merge:
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
dict1 = {
dict1 = {

Latest revision as of 23:06, 8 August 2023

Internal

TODO

Overview

A dictionary is a mutable collection of key-value pairs. The pairs can be accessed and modified. Each key is unique within the key set, and can be an instance of any immutable type: boolean, integer, float, tuple, string, etc., including None. There is a difference between a key that does not exist in the dictionary and a key that has been explicitly set to None - see Access with [] Syntax for more details. In other programming languages, the same data structure is referred to as "associative array" or "hash tables" or "hash maps".

The same underlying dictionary data structure can be referred by multiple variable names, which can be assigned with =, and if the structure is changed, the change will be reflected if any of the variables is used for access. To avoid this, make a copy of the dictionary with copy().

Dictionary type()

The function type() applied to a dictionary returns: <class 'dict'>

To check whether an instance is a dictionary:

i = ...
if type(i) is dict:
  ...

For dict subclasses:

i = ...
if isinstance(i, dict):
  ...

Key Discussion

The keys 1 and True are equivalent. Why?

None as Key

None is a valid key:

d = {}
d[None] = 10
d['A'] = 20
assert len(d.keys()) == 2
assert d[None] == 10
assert d['A'] == 20

Dictionaries and the __hash__() Function

Dictionaries and the __hash__() Function

Dictionaries and the == Operator

It would be nice if == recursively deep-compares maps, and it seems to be working, Research this and come up with a definitive conclusion.

Create a Dictionary

A new dictionary instance is declared using the {...} syntax. The curly braces are placed around comma-separated key: value pairs. The dictionary can be empty

d = {}

or it can be populated with values:

d = {'a': 'b', 1: 2}

It is good form to insert a space after :. A comma is tolerated after the last pair.

Converting Other Data Structures to a Dictionary

Any two-value sequences can be converted into a dictionary using the dict() function.

Examples:

# list of lists
l = [['a', 'b'], ['c', 'd'], ['e', 'f']]
d = dict(l) # will create {'a': 'b', 'c': 'd', 'e': 'f'}

# list of tuples
l = [('a', 'b'), ('c', 'd'), ('e', 'f')]
d = dict(l) # will create {'a': 'b', 'c': 'd', 'e': 'f'}

# tuple of two-item lists
l = (['a', 'b'], ['c', 'd'], ['e', 'f'])
d = dict(l) # will create {'a': 'b', 'c': 'd', 'e': 'f'}

# list of two-character strings
l = ['ab', 'cd', 'ef']
d = dict(l) # will create {'a': 'b', 'c': 'd', 'e': 'f'}

# tuple of two-character strings
l = ('ab', 'cd', 'ef')
d = dict(l) # will create {'a': 'b', 'c': 'd', 'e': 'f'}

Access a Dictionary

Test for Empty Dictionary

Empty dictionaries evaluate to False:

d1 = {}
if not d1:
    print("dictionary is empty")

Size of a Dictionary

The number of key is given by the len() function:

d = {...}
print(len(d))

Access Individual Elements

Individual dictionary elements can be accessed with the [] syntax or with the get() function.

Access with [] Syntax

The [] syntax can only be used with keys that exist in the dictionary:

d = {'a': 'b'}
print(d['a'])

An attempt to access an inexistent key will throw a KeyError exception. To avoid the exception, use get() instead, or test the existence of the key first.

However, a key can be explicitly set to None, and in this case, square bracket access to the key will return None:

d = {'a': None}
assert d['a'] is None

get() Function

The get() will return the associated value, or None if the key does not exist.

d = {'a': 'b'}
print(d.get('a')) # will display 'a'
print(d.get('no-such-key')) # will display None

The get() function allows for a second argument which will be returned instead of None in case the key does not exist:

d = {}
print(d.get('no-such-key', 'alternative')) # will display "alternative"

Safely Navigate a Complex Data Structure

Suggestions on how to safely recursively navigate a complex data structure:

Safely Navigate a Complex Data Structure

Test the Existence of a Key

The existence of the key can be tested with in:

if 'some-key' in d:
  print("key exists")

The non-existence is tested with this idiom:

if 'some-key' not in d:
  print("key does not exist")

get() can also be used:

if d.get('some-key') is not None:
  print("key exists")

Get All Keys with keys() Function

d = {'a': 'b', 'c': 'd'}
print(d.keys())

In Python 3, the keys() will return a dict_keys(), which is an iterable view of keys. This is useful with large dictionaries because the runtime does not use the time and the memory to create and store a list that might not be used. In case you need a list, use list() to wrap the result of keys().

list(d.keys())

Get All Values with values() Function

d = {'a': 'b', 'c': 'd'}
print(d.values())

In Python 3, the values() will return a dict_values(), which is an iterable view of values. This is useful with large dictionaries because the runtime does not use the time and the memory to create and store a list that might not be used. In case you need a list, use list() to wrap the result of values().

list(d.values())

Get All Key-Value Pairs with items() Function

d = {'a': 'b', 'c': 'd'}
print(d.items())

In Python 3, the items() will return a dict_items(), which is an iterable view of items. This is useful with large dictionaries because the runtime does not use the time and the memory to create and store a list that might not be used. Use use list() to wrap the result of items(). The list contains tuples of key and values.

for i in list(d.items()):
    print('key:', i[0])
    print('value:', i[1])

Equivalent:

for (k, v) in list(d.items()):
    print('key:', k)
    print('value:', v)

Iterate over a Dictionary

Use a for loop:

Iterate over Keys

Use the dictionary itself as argument of in. This is equivalent with iterating over the results of the keys() function.

d = {'a':'A', 'b':'B', 'c':'C'}
for k in d:
    print(k)

This will display:

a
b
c

Iterate over Values

Apply the in operator to the result of the values() function:

d = {'a':'A', 'b':'B', 'c':'C'}
for v in d.values():
    print(v)

This will display:

A
B
C

Iterate over Both Keys and Values

Apply the in operator to the result of the items() function. Each element is a tuple containing the key and the corresponding value:

d = {'a':'A', 'b':'B', 'c':'C'}
for kvt in d.items():
    print(kvt)

This will display:

('a', 'A')
('b', 'B')
('c', 'C')

Variables corresponding to the elements of the tuple can be assigned in one step, operation known as tuple unpacking, so the following syntax where we assign the key and the value to their corresponding variable is valid:

d = {'a':'A', 'b':'B', 'c':'C'}
for k, v in d.items():
    print('key:', k, 'value:', v)

This will display:

key: a value: A
key: b value: B
key: c value: C

Modify a Dictionary

Modify Individual Elements

Modification with [] Syntax

Individual elements can be modified with the [] syntax. If the key does not exist, the key-value pair will be added to the dictionary. If the key exists, the associated value will be updated:

d = {}
d['a'] = 'b'
print(d['a']) # will display "a"
d['a'] = 'c'
print(d['a']) # will display "c"

The syntax can be used with both literals and variables:

d = {}
d['a'] = 'b'
k = 'c'
d[k] = 'd'

Delete Individual Element

With del

d = {'a': 'b', 'c': 'd'}
del d['a']
print(d) # will display {'c': 'd'}

Note that if the key being deleted does not exist, the [] syntax will throw an KeyError exception. The solution is to guard with an if:

if 'a' in d:
  del d['a']

With pop()

pop() returns the value for the key provided as argument, and removes the key, if the key is in the dictionary. Throws KeyError if the key is not in the dictionary.

d = {'a': 'A'}
assert d.pop('a') == 'A'
assert len(d) == 0

The missing key can be recovered from the KeyError instance. The name of the missing key, as string, is propagated as the exception message as the first element of the args tuple, and can be accessed as such:

try:
   ...
except KeyError as e:
   print(e.args[0])

Delete All Items

Use the clear() function:

d = {...}
d.clear()

Combine Dictionaries

Two dictionaries can be combined with update():

d1 = {'a': 'b'}
d2 = {'c': 'd'}
d1.update(d2) # d1 will become {'a': 'b', 'c': 'd'}, d2 will remain unchanged

If the same key exists in both dictionaries, the value associated with the key in the second dictionary will take precedence.

Copy a Dictionary

d1 = {'a': 'b'}
d2 =d1.copy()
d1['a'] = 'c'
print(d1['a']) # will display 'c'
print(d2['a']) # will display 'b'

Recursively Merge Dictionaries

This only works on shallow trees, it fails to do deep recursive merge:

dict1 = {
    'a': 'A',
}

dict2 = {
    'b': 'B',
    'c': ['C', 'D']
}

dict1.update(dict2)

assert dict1 == {
    'a': 'A',
    'b': 'B',
    'c': ['C', 'D']
}

Handle Missing Keys with setdefault() and defaultdict()

TODO IPy Page 116.

Ordered Dictionary

TODO IPy Page 120.

Subclass a Dictionary

class StorageClass(dict):
    def __init__(self, storage_class_config):
        super().__init__()
        name = None
        if storage_class_config:
            name = storage_class_config.get('name')
        if not name:
            raise ValueError("invalid 'config.csi_driver.storage_classes' element: missing 'name'")
        self['name'] = name
        self['volumeBindingMode'] = 'WaitForFirstConsumer'
        self['reclaimPolicy'] = 'Delete'
        self['allowVolumeExpansion'] = True
        self['parameters'] = {
            'type': 'gp3',
            'encrypted': 'true',
            'csi.storage.k8s.io/fstype': 'ext4',
            'throughput': '125',
            'iops': '3000'
        }