1

My variable L1 looks this:

[{'distance': 1.9999,
  'breakEvenDistance': 1.9329,
  'max': 0.0010342833053929787},
 {'distance': 1.9251,
  'breakEvenDistance': 2.0669999999999997,
  'max': 0.0011183923895419084,
  'min': 0.0010342833053929787},
 {'distance': 1.8802,
  'breakEvenDistance': 1.6758,
  'max': 0.0011927892918825562,
  'min': 0.0011183923895419084},
 {'distance': 1.8802,
  'breakEvenDistance': 1.5956,
  'max': 0.0012522046577102665,
  'min': 0.0011927892918825562}]

What I need is to remove the last 'max' in my list! I tried to check the doc, used the function del on L1[-1]['max'] but did not work.. any idea or clue? thanks!

Viktor.w
  • 1,787
  • 2
  • 20
  • 46
  • 3
    Can't reproduce, works just fine. – meowgoesthedog Mar 12 '19 at 11:38
  • 2
    works just fine del L1[-1]['max'] does the job – rachid el kedmiri Mar 12 '19 at 11:39
  • Do you want to remove `max` key from every element? deleting last dict max just works fine using `del L1[-1]['max'] ` – Rahul Mar 12 '19 at 11:43
  • @viktor.w it's unclear whether you want to remove the `max` key from the last dict, or the last dict entirely, or something else. If the first, you've had answers. If the second, you can use `lst.pop()` or `del lst[-1]` to remove the last element. If otherwise, you'll need to clarify. – Masklinn Mar 12 '19 at 11:45

1 Answers1

3

-1 to get the last element of the dict:

del dict_[-1]['max']
print(dict_)

OUTPUT:

[
 {'distance': 1.9999, 'breakEvenDistance': 1.9329, 'max': 0.0010342833053929787},
 {'distance': 1.9251, 'breakEvenDistance': 2.0669999999999997, 'max': 0.0011183923895419084, 
  'min': 0.0010342833053929787}, 
 {'distance': 1.8802, 'breakEvenDistance': 1.6758, 'max': 0.0011927892918825562, 
  'min': 0.0011183923895419084}, 
 {'distance': 1.8802, 'breakEvenDistance': 1.5956, 
  'min': 0.0011927892918825562}
]
DirtyBit
  • 16,613
  • 4
  • 34
  • 55