2

I am having a list in below format. How can i create another list from the existing one with just selected elements.

[{'UserDiscount': 0.0, 'CostTotalInvEOPAmount': 940.0, 'WeekEndingData': u'2016-10-08', 'WeeksOnHand': 0.0, 'UnitTotalInvEOPQuantity': 250.0, 'WeeksOfSales': 0.0, 'UnitCostAmount': 3.76, 'Week': u'2016 Wk 36', 'CostReceiptAmount': 940.0, 'UnitSalesQuantity': 0.0, 'UnitReceiptQuantity': 250.0, 'Demand': 0.0, 'InventoryBOP': 0.0, 'PEMDiscount': 0.0, 'ElasticLift': 0.0, 'StoreCount': 0, 'PriceStatus': 4, 'UnitOnOrderQuantity': None, 'ReceiptSizeContributions': [{u'sizeId': u'e1656ac7-1cc1-40ce-b485-989bba9d758d', u'contribution': 1.0}], 'CostSalesAmount': 0.0, 'LifeCycleProperties': {u'IsAtRegularPrice': False, u'IsAtMarkdown': False, u'IsFinished': False, u'IsPreSeason': True}, 'MardownDiscount': 0.0, 'RecommendedReceipt': 250.0, 'RecommendedReceiptSizeContributions': [{u'sizeId': u'e1656ac7-1cc1-40ce-b485-989bba9d758d', u'contribution': 1.0}], 'UnitTotalInvBOPQuantity': 0.0, 'CostOnOrderAmount': None, 'InventoryEOP': 250.0, 'CostTotalInvBOPAmount': 0.0, 'Receipt': 250.0, 'Sales': 0.0, 'LostSales': 0.0, 'TotalDiscount': 0.0, 'RetailSalesAmount': 0.0},
 {'UserDiscount': 0.0, 'CostTotalInvEOPAmount': 940.0, 'WeekEndingData': u'2016-10-15', 'WeeksOnHand': 0.0, 'UnitTotalInvEOPQuantity': 250.0, 'WeeksOfSales': 15.784951285314385, 'UnitCostAmount': 3.76, 'Week': u'2016 Wk 37', 'CostReceiptAmount': 0.0, 'UnitSalesQuantity': 0.0, 'UnitReceiptQuantity': 0.0, 'Demand': 0.0, 'InventoryBOP': 250.0, 'PEMDiscount': 0.0, 'ElasticLift': 0.0, 'StoreCount': 0, 'PriceStatus': 4, 'UnitOnOrderQuantity': None, 'ReceiptSizeContributions': [], 'CostSalesAmount': 0.0, 'LifeCycleProperties': {u'IsAtRegularPrice': False, u'IsAtMarkdown': False, u'IsFinished': False, u'IsPreSeason': True}, 'MardownDiscount': 0.0, 'RecommendedReceipt': 0.0, 'RecommendedReceiptSizeContributions': [], 'UnitTotalInvBOPQuantity': 250.0, 'CostOnOrderAmount': None, 'InventoryEOP': 250.0, 'CostTotalInvBOPAmount': 940.0, 'Receipt': 0.0, 'Sales': 0.0, 'LostSales': 0.0, 'TotalDiscount': 0.0, 'RetailSalesAmount': 0.0}]

My new list will having below elements.

[{'UserDiscount': 0.0, 'CostTotalInvEOPAmount': 940.0, 'WeekEndingData': u'2016-10-08', 'WeeksOnHand': 0.0, 'UnitTotalInvEOPQuantity': 250.0, 'WeeksOfSales': 0.0, 'UnitCostAmount': 3.76, 'Week': u'2016 Wk 36', 'CostReceiptAmount': 940.0, 'UnitSalesQuantity': 0.0, 'UnitReceiptQuantity': 250.0, 'Demand': 0.0, 'InventoryBOP': 0.0, 'PEMDiscount': 0.0, 'ElasticLift': 0.0, 'StoreCount': 0, 'PriceStatus': 4, 'UnitOnOrderQuantity': None, 'CostSalesAmount': 0.0,  'RecommendedReceipt': 250.0, 'RetailSalesAmount': 0.0},
 {'UserDiscount': 0.0, 'CostTotalInvEOPAmount': 940.0, 'WeekEndingData': u'2016-10-15', 'WeeksOnHand': 0.0, 'UnitTotalInvEOPQuantity': 250.0, 'WeeksOfSales': 15.784951285314385, 'UnitCostAmount': 3.76, 'Week': u'2016 Wk 37', 'CostReceiptAmount': 0.0, 'UnitSalesQuantity': 0.0, 'UnitReceiptQuantity': 0.0, 'Demand': 0.0, 'InventoryBOP': 250.0, 'PEMDiscount': 0.0, 'ElasticLift': 0.0, 'StoreCount': 0, 'PriceStatus': 4, 'UnitOnOrderQuantity': None,  'CostSalesAmount': 0.0, 'RecommendedReceipt': 0.0, 'RetailSalesAmount': 0.0}]
6
  • What is the method behind the selection? This list has only 2 elements, so i guess you want to do something with the dictionaries inside the list. Commented Dec 7, 2016 at 8:47
  • Hi gabor, can you please reframe your question. what does it means by method behind the selection. is it about selecting the elements in the new list? Commented Dec 7, 2016 at 8:49
  • Yes gabor, I want to filter our few elements from the inner dictionaries Commented Dec 7, 2016 at 8:51
  • As i said in your list you only have 2 elements. You can refer to them as list[0] or list[1]. Your question is very unclear, and from your description it is unsolvable. I think you want to do something with the dictionaries inside the list, but it is unclear what you want to do. Commented Dec 7, 2016 at 8:51
  • What did you try till now? Stackoverflow won't give you the code, you have to come to the conclusion to yourself ;) Commented Dec 7, 2016 at 8:51

2 Answers 2

2

You have a list with two dictionaries. To filter the dictionaries you can try

    keep=[key1,key2] #keys you wanna keep
    newList = []
    for item in mylist:
        d = dict((key,value) for key, value in item.iteritems() if key in keep)
        newlist.append(d)
    del mylist

Also using funcy you can do a

   import funcy
   mydict={1:1,2:2,3:3}
   keep=[1,2]
   funcy.project(mydict,keep)
=> {1: 1, 2: 2}

which is much prettier imho.

Sign up to request clarification or add additional context in comments.

6 Comments

How to check if all the list elements , i.e newlist[0], newlist[1] contains equal number of keys. if a certain key is missing from newlist[1], i need to delete that element from the list. i.e newlist.pop[1]
@occasionalvisitor to See how many keys are there do a len(dict.keys()). To remove on missing key do a try: dict[key] except KeyError: #pop here.
d = dict((key, value) for key, value in items.iteritems() if all(cols in items for cols in filter_columns)) is checking if all required keys are present in dict. if yes i am adding to new list, but if any of the keys are not present it is appending an empty dict to new list. Any help??
Add an else next to the if to do what you want
You mean something like this?? for items in filtered_list: d = dict((key, value) for key, value in items.iteritems() if all(cols in items for cols in filter_columns)) final_list.append(d) else:
|
1

You could use the list comprehension https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

[l for l in your_list if l['UserDiscount'] >= 1 ]
[{'UserDiscount': l['UserDiscount'],'CostTotalInvEOPAmount': l['CostTotalInvEOPAmount']}  for l in your_list ]

Using this way you can filter the elements in your list and change the structure of your dicts in the list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.