init_value: Any = 1,
inc_function: Callable[..., Any] = lambda x: x + 1,
) -> bool:
- """
- Initialize a dict value (if it doesn't exist) or increments it (using the
+ """Initialize a dict value (if it doesn't exist) or increments it (using the
inc_function, which is customizable) if it already does exist.
+ See also :py:class:`defaultdict`
+ (https://docs.python.org/3/library/collections.html#collections.defaultdict)
+ for a more pythonic alternative.
+
Args:
d: the dict to increment or initialize a value in
key: the key to increment or initialize
- init_value: default initial value
+ init_value: default initial value (see also :meth:`dict.setdefault`)
inc_function: Callable use to increment a value
Returns:
False
>>> d
{'test': 2, 'ing': 1}
+
"""
if key in d.keys():
d[key] = inc_function(d[key])
Returns:
A dict composed of zipping the keys list and values list together.
+ Raises:
+ ValueError: if keys and values lists not the same length.
+
>>> k = ['name', 'phone', 'address', 'zip']
>>> v = ['scott', '555-1212', '123 main st.', '12345']
>>> parallel_lists_to_dict(k, v)
{'name': 'scott', 'phone': '555-1212', 'address': '123 main st.', 'zip': '12345'}
"""
if len(keys) != len(values):
- raise Exception("Parallel keys and values lists must have the same length")
+ raise ValueError("Parallel keys and values lists must have the same length")
return dict(zip(keys, values))