0

I am asked to extract the dates of dictionary in which the values exceed the input threshold.

See code beneath:

def get_dates(prices,threshold):
    {k:v for  k,v  in prices  if threshold>130}
    

prc = [
    { 'price': 1279.79, 'date': '2020-01-01' },
    { 'price': 139.01, 'date': '2020-01-02' },
    { 'price': 134.3, 'date': '2020-01-03' },
    { 'price': 120.99, 'date': '2020-01-04' }
]

get_dates(prc, 130.0)

Ideally, the function should return the respective date on which the price is beyond the threshold (130). My code does not however return anything.

Kash
  • 99
  • 4
  • 1
    the function returns `None`... pass the list of dictionaries to the function (as argument), make a for loop and check the condition. Store the results in a list and return it. – cards Nov 06 '22 at 20:15

3 Answers3

1

You can use list comprehensions and return dicts if the price is larger than the threshold.

Why dict.get(key) instead of dict[key]?

# we use 'dict.get' with default value '0' for price if don't exists.
def get_dates(prices,threshold):
    return [dct['date'] for dct in prices if dct.get('price', 0) > threshold]
            

prc = [
    { 'price': 1279.79, 'date': '2020-01-01' },
    { 'price': 139.01, 'date': '2020-01-02' },
    { 'price': 134.3, 'date': '2020-01-03' },
    { 'price': 120.99, 'date': '2020-01-04' }
]

get_dates(prc, 130.0)

Output:

['2020-01-01', '2020-01-02', '2020-01-03']
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • Your solution worked! Thank you. What, if I only want to extract the key "date" alone? – Kash Nov 06 '22 at 20:18
  • @Kash, you're welcome, the answer updated ;) – I'mahdi Nov 06 '22 at 20:22
  • The 0 after "price" was not necessary btw to execute the same output. – Kash Nov 06 '22 at 20:24
  • @Kash, test with this : `prc = [ { 'price': 134.3, 'date': '2020-01-03' }, { 'date': '2020-01-04' } ]` if you don't use `get` you get an error. – I'mahdi Nov 06 '22 at 20:27
  • @Kash, we use `dict.get()` for the condition if the key doesn't exist in `dict` and we don't get any error if the key doesn't exist and returns the default value then the code run correctly always. – I'mahdi Nov 06 '22 at 20:29
  • Not raising an exception isn't the same thing as running "correctly". If the expectation is that the input should always have a `price` key, the *correct* thing is arguably to immediately raise an error if that's not the case, in order to make debugging easier. Using the `[]` operator gets you that for free. – Samwise Nov 06 '22 at 23:53
1

First of all there is no return keyword in your function. Then do it like this

def get_dates(prices, threshold) :
    dates=[]
    for dict in prices:
        if dict['price']>threshold:
            dates.append(dict['date'])
    return dates

This will return an array of dates Then to get this array use dates=get_dates(prc, 130)

Dante
  • 165
  • 8
0

To return the dates you want, use a list comprehension that gets the date key from each dict where price is above threshold:

def get_dates(prices,threshold):
    return [d['date'] for d in prices if d['price'] > threshold]

prc = [
    { 'price': 1279.79, 'date': '2020-01-01' },
    { 'price': 139.01, 'date': '2020-01-02' },
    { 'price': 134.3, 'date': '2020-01-03' },
    { 'price': 120.99, 'date': '2020-01-04' }
]

print(get_dates(prc, 130.0))  # ['2020-01-01', '2020-01-02', '2020-01-03']
Samwise
  • 68,105
  • 3
  • 30
  • 44