Assuming I have the following:
[
{"sku": "ZZZ", "name":"None name","shelf": 10},
{"sku": "AAA", "name":"One name","shelf": 10},
{"sku": "BBB", "name":"The name", "shelf": None},
{"sku": "CCC", "name":"A name"}
]
I am trying to find the best (most elegant maybe) way to:
- Add "shelf": 'Default' when missing or set to
None
- Split the list per shelf, so the one above should give out two named lists: one for Default and one for '10' This is the desired output:
[
{"10":[
{"sku": "ZZZ", "name":"None name"},
{"sku": "AAA", "name":"One name"}]
},
{"default":[
{"sku": "CCC", "name":"A name"},
{"sku": "BBB", "name":"The name"]
}
]
Using pydash.collections.for_each(initial_list,reorganize)
I can sort the first problem, but I am not sure how to deal with the second.
def reorganize(x):
if 'shelf' not in x: x['shelf'] = 'default'
if x['shelf'] is None: x['shelf'] = 'default'
I also do not thing this is the best way to solve the problem. Reason for pivoting the list is because I need to call an API which needs the shelf as parameter and cannot accept multiple shelf at the same time (but accepts multiple SKUs).