LOD#
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 lod_ to a dict using (key1,key2) tuple as new dict key, and the dict as a value
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)
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_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")
4
lod_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")
2
lod_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#
Prints a list of dictionaries in a tabulated way
from pydicts.lod import lod_print
lod_=[{"a":1, "b":4},{"a":2, "b":None}]
lod_print(lod_)
+-----+-----+
| 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_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")
3
lod_sum_negatives#
Help on function lod_sum_negatives in module pydicts.lod:
lod_sum_negatives(lod_, key)
lod_sum_positives#
Help on function lod_sum_positives in module pydicts.lod:
lod_sum_positives(lod_, key)