dod2lod¶
Converts a dictionary of dictionaries (dod) to a list of dictionaries (lod), ignoring dictionary keys
from pydicts import lod
dod={'key2': {'a': 1, 'b': 2}, 'key1': {'a': 1, 'b': 2}}
lod.dod2lod(dod)[{'a': 1, 'b': 2}, {'a': 1, 'b': 2}]lod2dictkv¶
Converts a list of dictionaries (lod) to a dictionary using a key for the new key and another key for its value
from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}]
lod.lod2dictkv(lod_,"a","b"){1: 2, 3: 4}lod2dod¶
Converts a list of dictionaries (lod) to a dictionary of dictionaries (dod) using a key as the dictionary key. This is a fast method to access dictionaries.
from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}]
lod.lod2dod(lod_,"b"){2: {'a': 1, 'b': 2}, 4: {'a': 3, 'b': 4}}lod2dod_tuple¶
Help on function lod2dod_tuple in module pydicts.lod:
lod2dod_tuple(lod_, key1, key2)
Converts a list of dictionaries into a dictionary of dictionaries (DoD),
using a tuple of two specified keys from each dictionary as the new DoD's key.
Args:
lod_ (list): The list of dictionaries.
key1 (str): The first key in the original dictionaries to use for the tuple key.
key2 (str): The second key in the original dictionaries to use for the tuple key.
Returns:
dict: A dictionary of dictionaries with tuple keys.
lod2list¶
Converts a list of dictionaries (lod) to list using all values of key
from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}]
lod.lod2list(lod_,"a")[1, 3]lod2list_distinct¶
Converts a list of dictionaries (lod) to list using all distinct values of key
from pydicts import lod
lod_= [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 3, 'b': 4}]
print("lod2list:",lod.lod2list(lod_,"a"))
print("lod2list_distinct:",lod.lod2list_distinct(lod_,"a"))lod2list: [1, 3, 3]
lod2list_distinct: [1, 3]
lod2lol¶
Converts a list of dictionaries (lod) to a list of lists
If you don’t select keys (Supposed ordereddict)
from pydicts import lod
lod_= [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 3, 'b': 4}]
lod.lod2lol(lod_)[[1, 2], [3, 4], [3, 4]]You can select your keys in the order you need
from pydicts import lod
lod_= [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 3, 'b': 4}]
lod.lod2lol(lod_,["b","a"])[[2, 1], [4, 3], [4, 3]]lod2lood¶
Help on function lod2lood in module pydicts.lod:
lod2lood(ld, keys)
Converts a list of dictionaries into a list of OrderedDicts (List of OrderedDicts - LoOD),
containing only the specified keys.
Args:
ld (list): The list of dictionaries.
keys (list): A list of keys to include in each OrderedDict.
Returns:
list: A list of OrderedDicts.
lod_aggregate_sum¶
Groups a List of Dictionaries (LOD) by all keys except one, summing the values of that key for duplicate records.
from pydicts import lod
lod_ = [{'id': 1, 'v': 10}, {'id': 1, 'v': 5}, {'id': 2, 'v': 20}]
print("Before")
lod.lod_print(lod_)
lod_ = lod.lod_aggregate_sum(lod_, 'v')
print("After")
lod.lod_print(lod_)Before
+------+-----+
| id | v |
|------+-----|
| 1 | 10 |
| 1 | 5 |
| 2 | 20 |
+------+-----+
After
+------+-----+
| id | v |
|------+-----|
| 1 | 15 |
| 2 | 20 |
+------+-----+
lod_average¶
Calculates average from all values is the list of dictionaries with the key passed as parameter.
from pydicts.lod import lod_print,lod_average
lod_=[{"a":1, "b":2},{"a":3, "b":2},{"a":5, "b":2}]
lod_print(lod_)
print(lod_average(lod_,"a"))+-----+-----+
| a | b |
|-----+-----|
| 1 | 2 |
| 3 | 2 |
| 5 | 2 |
+-----+-----+
3.0
lod_average_ponderated¶
Calculates average ponderated from all values is the list of dictionaries with the key passed as parameter, and the weight passed as a parameter.
from pydicts.lod import lod_print,lod_average_ponderated
lod_=[{"a":1, "b":1},{"a":3, "b":2},{"a":5, "b":3}]
lod_print(lod_)
print(lod_average_ponderated(lod_,"a","b"))+-----+-----+
| a | b |
|-----+-----|
| 1 | 1 |
| 3 | 2 |
| 5 | 3 |
+-----+-----+
2.4444444444444446
lod_calculate¶
Makes calculations inside dictionary iterations
from pydicts import lod
lod_= [{'a': 1, 'b': 2, 'c':5}, {'a': 3, 'b': 4, 'c': 6}, {'a': 3, 'b': 4, 'c': 7}]
lod.lod_calculate(lod_, "d", lambda d, index: d['a']+d['b']+d['c'])[{'a': 1, 'b': 2, 'c': 5, 'd': 8},
{'a': 3, 'b': 4, 'c': 6, 'd': 13},
{'a': 3, 'b': 4, 'c': 7, 'd': 14}]lod_clone¶
Makes a clone of the list of dictionaries with a new list and new dictionaries
lod_count¶
Count values in a list of dictioanries
from pydicts import lod
lod_= [{'a': 1, 'b': 2, 'c':5}, {'a': 3, 'b': 4, 'c': 6}, {'a': 3, 'b': 4, 'c': None}]
a=lod.lod_count(lod_,lambda d, index: d["a"]>3)
b=lod.lod_count(lod_,lambda d, index: d["c"] is None)
c=lod.lod_count(lod_,lambda d, index: index>=0)
print(a,b,c)0 1 3
lod_filter_dictionaries¶
Create a new lod leaving filtering dictionaries that returns True to lambda funcion
from pydicts import lod
lod_= [{'a': 1, 'b': 2, 'c':5}, {'a': 3, 'b': 4, 'c': 6}, {'a': 3, 'b': 4, 'c': 7}]
lod.lod_filter_dictionaries(lod_, lambda d, index: index >0 and d['c']> 6)[{'a': 3, 'b': 4, 'c': 7}]lod_filter_keys¶
Creates a new list of dictionaries with dictionaries that only have the keys passed as parameters
from pydicts import lod
lod_= [{'a': 1, 'b': 2, 'c':5}, {'a': 3, 'b': 4, 'c': 6}, {'a': 3, 'b': 4, 'c': 7}]
lod.lod_print(lod_)
new_lod=lod.lod_filter_keys(lod_, ["a", "c"])
lod.lod_print(new_lod)+-----+-----+-----+
| a | b | c |
|-----+-----+-----|
| 1 | 2 | 5 |
| 3 | 4 | 6 |
| 3 | 4 | 7 |
+-----+-----+-----+
+-----+-----+
| a | c |
|-----+-----|
| 1 | 5 |
| 3 | 6 |
| 3 | 7 |
+-----+-----+
lod_has_key¶
Returns a boolean. Checks if list of dictionaries has a key
from pydicts.lod import lod_has_key
lod=[{"a":1, "b":4},{"a":2, "b":5}]
print(lod_has_key(lod,"a"))
print(lod_has_key(lod,"d"))
print(lod_has_key([ ],"d"))True
False
False
lod_join¶
Help on function lod_join in module pydicts.lod:
lod_join(lod1, lod2, calc_key, operation='+')
Joins two lists of dictionaries (lod) by all keys except the calc_key, performing an arithmetic operation on the selected calc_key.
The list maintains the order of elements in lod1, followed by any new elements from lod2.
If the value of `calc_key` is None or missing in any dictionary, it is treated as 0 for the calculation.
Args:
lod1 (list): The first list of dictionaries.
lod2 (list): The second list of dictionaries.
calc_key (str): The key on which to perform the calculation.
operation (str, optional): The operation to perform ("+" or "-"). Defaults to "+".
Returns:
list: A new list of dictionaries with the combined results.
from pydicts import lod
lod1 = [
{"id": 1, "value": 10, "name": "A"},
{"id": 2, "value": 20, "name": "B"}
]
lod2 = [
{"id": 1, "value": 5, "name": "A"},
{"id": 3, "value": 15, "name": "C"}
]
print("Sum:")
lod.lod_print(lod.lod_join(lod1, lod2, calc_key="value", operation="+"))
print("\nSubtraction:")
lod.lod_print(lod.lod_join(lod1, lod2, calc_key="value", operation="-"))Sum:
+------+---------+--------+
| id | value | name |
|------+---------+--------|
| 1 | 15 | A |
| 2 | 20 | B |
| 3 | 15 | C |
+------+---------+--------+
Subtraction:
+------+---------+--------+
| id | value | name |
|------+---------+--------|
| 1 | 5 | A |
| 2 | 20 | B |
| 3 | -15 | C |
+------+---------+--------+
lod_keys¶
Returns a list with the keys of the first dictionary in the list
from pydicts import lod
a=[{"a":1,"b":2}, {"a":3,"b":4}]
lod.lod_keys(a)['a', 'b']lod_max_value¶
Returns the maximum value of a key in a list of dictionaries (lod)
from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}]
lod.lod_max_value(lod_,"b")4lod_median¶
Calculates median from all values is the list of dictionaries with the key passed as parameter.
from pydicts.lod import lod_print,lod_median
lod_=[{"a":1, "b":2},{"a":3, "b":2},{"a":5, "b":2}]
lod_print(lod_)
print(lod_median(lod_,"a"))+-----+-----+
| a | b |
|-----+-----|
| 1 | 2 |
| 3 | 2 |
| 5 | 2 |
+-----+-----+
3
lod_min_value¶
Returns the minimum value of a key in a list of dictionaries (lod)
from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}]
lod.lod_min_value(lod_,"b")2lod_order_by¶
Orders a list of dictionaries (lod) by a key. You can make reverse orders and set None values at the top or the bottom of the ordered list
from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}, {"a":3, "b": None}]
lod.lod_order_by(lod_,"b")[{'a': 3, 'b': None}, {'a': 1, 'b': 2}, {'a': 3, 'b': 4}]from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}, {"a":3, "b": None}]
lod.lod_order_by(lod_,"b",reverse=True)[{'a': 3, 'b': None}, {'a': 3, 'b': 4}, {'a': 1, 'b': 2}]from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}, {"a":3, "b": None}]
lod.lod_order_by(lod_,"b",reverse=True, none_at_top=False)[{'a': 3, 'b': 4}, {'a': 1, 'b': 2}, {'a': 3, 'b': None}]lod_print¶
Help on function lod_print in module pydicts.lod:
lod_print(lod_, number=None, align=None)
Prints a list of dictionaries with tabulate module.
- lod_: List of dictionaries
- number: Number of dictionaries in the list to print. If None prints all lod_. (defaults to None)
- align: Cells alignment with tabultate sintaxis. (defaults to None). Possible column alignments are:
right, center, left, decimal (only for numbers), and None (to disable alignment). Omitting an
alignment uses the default.
from pydicts.lod import lod_print
lod_=[{"a":1, "b":4},{"a":2, "b":None}]
lod_print(lod_, align=['center', 'right'])+-----+-----+
| a | b |
|-----+-----|
| 1 | 4 |
| 2 | |
+-----+-----+
lod_rename_key¶
Renames a key name with other given as a parameter
from pydicts import lod
lod_=[{"a":1,"b":2}, {"a":3,"b":4}, {"a":3, "b": 4}]
lod.lod_rename_key(lod_,"b","new")[{'a': 1, 'new': 2}, {'a': 3, 'new': 4}, {'a': 3, 'new': 4}]lod_remove_duplicates¶
Removes duplicates (dictionaries with the same keys and values) from a list of dictionaries.
from pydicts.lod import lod_print, lod_remove_duplicates
lod_=[{"a":1, "b":2},{"a":1, "b":2},{"a":1, "b":2}]
lod_print(lod_)
lod_=lod_remove_duplicates(lod_)
lod_print(lod_)+-----+-----+
| a | b |
|-----+-----|
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
+-----+-----+
+-----+-----+
| a | b |
|-----+-----|
| 1 | 2 |
+-----+-----+
lod_remove_key¶
Removes a key in all dictionaries in the list of dictionaries
from pydicts.lod import lod_print, lod_remove_key
lod=[{"a":1, "b":4},{"a":2, "b":None}]
lod_print(lod)
lod_remove_key(lod,"b")
lod_print(lod)+-----+-----+
| a | b |
|-----+-----|
| 1 | 4 |
| 2 | |
+-----+-----+
+-----+
| a |
|-----|
| 1 |
| 2 |
+-----+
lod_reorder_keys¶
Help on function lod_reorder_keys in module pydicts.lod:
lod_reorder_keys(lod_, keys)
Reorders the keys of all dictionaries in a list according to a specified list of keys.
Keys not present in the `keys` list will be appended at the end of each dictionary.
Args:
lod_ (list): The list of dictionaries to modify.
keys (list): A list of strings representing the desired order of keys.
Returns:
list: A new list of dictionaries with reordered keys.
from pydicts.lod import lod_print, lod_remove_key
lod_ = [{"c": 3, "a": 1, "b": 2}, {"c": 6, "a": 4, "b": 5}]
lod_print(lod_)
new_lod = lod.lod_reorder_keys(lod_, ["a", "b"])
lod_print(new_lod)+-----+-----+-----+
| c | a | b |
|-----+-----+-----|
| 3 | 1 | 2 |
| 6 | 4 | 5 |
+-----+-----+-----+
+-----+-----+-----+
| a | b | c |
|-----+-----+-----|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
+-----+-----+-----+
lod_sum¶
Sums all values from a lod key. None values are ignored by default
from pydicts.lod import lod_sum
lod=[{"a":1, "b":4},{"a":2, "b":None}]
lod_sum(lod,"a")3lod_sum_negatives¶
Help on function lod_sum_negatives in module pydicts.lod:
lod_sum_negatives(lod_, key)
Calculates the sum of negative values for a specified key across all dictionaries in a list.
None and positive values are ignored.
Args:
lod_ (list): The list of dictionaries.
key (str): The key whose values are to be summed.
Returns:
(int, float, Decimal): The sum of the negative values.
lod_sum_positives¶
Help on function lod_sum_positives in module pydicts.lod:
lod_sum_positives(lod_, key)
Calculates the sum of positive values for a specified key across all dictionaries in a list.
None and negative values are ignored.
Args:
lod_ (list): The list of dictionaries.
key (str): The key whose values are to be summed.
Returns:
(int, float, Decimal): The sum of the positive values.