0

I have a dictionary and I want to delete every first row from the dictionary. e.g I want to delete this across the dictionary. {'date': '', 'obsolete': False}.


[{'date': '', 'obsolete': False},
 {'date': '2021-01-02',
  'obsolete': True,
  'sku': '100940478',
  'warehouse_location': 'lagos',
  'quantity': '23'},
 {'date': '', 'obsolete': False},
 {'date': '2020-11-09',
  'obsolete': False,
  'sku': '100940479',
  'warehouse_location': 'lagos',
  'quantity': '84'},
 {'date': '', 'obsolete': False},
 {'date': '2021-01-15',
  'obsolete': True,
  'sku': '100940480',
  'warehouse_location': 'oyo',
  'quantity': '11'}]
martineau
  • 119,623
  • 25
  • 170
  • 301
chuky pedro
  • 756
  • 1
  • 8
  • 26
  • 1
    Does this answer your question? [How can I remove a key from a Python dictionary?](https://stackoverflow.com/questions/11277432/how-can-i-remove-a-key-from-a-python-dictionary) – MrJuicyBacon Jul 21 '21 at 00:02
  • What you have is a *list* of dictionaries, so it's unclear what you mean about delete every first row from the dictionary. Which or what dictionary? – martineau Jul 21 '21 at 01:47

3 Answers3

1

There is not row notion in dictionaries. You can remove certain keys though. see How can I remove a key from a Python dictionary?

Saeid
  • 51
  • 3
1

I think this might be what you what to do. It removes all the dictionaries in the list whose contents are {'date': '', 'obsolete': False}:

from pprint import pprint


OBSOLETE = {'date': '', 'obsolete': False}

dict_list = [{'date': '', 'obsolete': False},
             {'date': '2021-01-02',
              'obsolete': True,
              'sku': '100940478',
              'warehouse_location': 'lagos',
              'quantity': '23'},
             {'date': '', 'obsolete': False},
             {'date': '2020-11-09',
              'obsolete': False,
              'sku': '100940479',
              'warehouse_location': 'lagos',
              'quantity': '84'},
             {'date': '', 'obsolete': False},
             {'date': '2021-01-15',
              'obsolete': True,
              'sku': '100940480',
              'warehouse_location': 'oyo',
              'quantity': '11'}]

cleaned_list = [d for d in dict_list if d != OBSOLETE]

pprint(cleaned_list, sort_dicts=False)

Output:

[{'date': '2021-01-02',
  'obsolete': True,
  'sku': '100940478',
  'warehouse_location': 'lagos',
  'quantity': '23'},
 {'date': '2020-11-09',
  'obsolete': False,
  'sku': '100940479',
  'warehouse_location': 'lagos',
  'quantity': '84'},
 {'date': '2021-01-15',
  'obsolete': True,
  'sku': '100940480',
  'warehouse_location': 'oyo',
  'quantity': '11'}]
martineau
  • 119,623
  • 25
  • 170
  • 301
0

Lets first assign your array to a variable:

data = [
    {'date': '', 'obsolete': False},
    {'date': '2021-01-02', 'obsolete': True, 'sku': '100940478', 'warehouse_location': 'lagos', 'quantity': '23'},
    {'date': '', 'obsolete': False},
    {'date': '2020-11-09', 'obsolete': False, 'sku': '100940479', 'warehouse_location': 'lagos', 'quantity': '84'},
    {'date': '', 'obsolete': False}, 
    {'date': '2021-01-15', 'obsolete': True, 'sku': '100940480', 'warehouse_location': 'oyo', 'quantity': '11'}
]

Now we can iterate through each element (dictionary) in the data array, removing the desired elements based on their key name, not index, as elements of dictionaries are unordered:

for dict_item in data:
    dict_item.pop('date')
    dict_item.pop('obsolete')

And then print out the data variable to see the result:

print(data) # Will output the array of dictionaries without the 'date' and 'obsolete' keys