-1

I´m stuck (as usual) and can't find a working solution om my problem. I´m new into this.

I have a list like myList {'00:00': 24.3, '01:00': 31.22, '02:00': 30.69, '03:00': 30.16, '04:00': 31.04, '05:00': **153.48**, '06:00': **140.48**, '07:00': **289.78**} and want to get rid of all key/values in the list if the value is over a certain threshold ex 100.00 (marked above).

The list I want back would be like: newList {'00:00': 24.3, '01:00': 31.22, '02:00': 30.69, '03:00': 30.16, '04:00': 31.04}

How do I code it?

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • Since you're learning - You're working with `dict`, not `list`. Have you tried any code? And if so, please show it. Do you have an inkling of how to start, in terms of the logic? – wkl Aug 16 '22 at 20:16

1 Answers1

2

This is not a list but a dictionary.

d = {'00:00': 24.3, '01:00': 31.22, '02:00': 30.69, '03:00': 30.16, '04:00': 31.04, '05:00': 153.48, '06:00': 140.48, '07:00': 289.78}

Use a dictionary comprehension:

d = {k:v for k,v in d.items() if v < 100}

Output:

{'00:00': 24.3, '01:00': 31.22, '02:00': 30.69, '03:00': 30.16, '04:00': 31.04}
mozway
  • 194,879
  • 13
  • 39
  • 75