0

I have a list as shown below:

[{'id': 'id_123',
  'type': 'type_1',
  'created_at': '2020-02-12T17:45:00Z'},
 {'id': 'id_124',
  'type': 'type_2',
  'created_at': '2020-02-12T18:15:00Z'},
 {'id': 'id_125',
  'type': 'type_1',
  'created_at': '2020-02-13T19:43:00Z'},
 {'id': 'id_126',
  'type': 'type_3',
  'created_at': '2020-02-13T07:00:00Z'}]

I am trying to find how many times type : type_1 occurs and what is the earliest created_at timestamp in that list for type_1

5 Answers 5

1

We can achieve this in several steps.

To find the number of times type_1 occurs we can use the built-in filter in tandem with itemgetter.

from operator import itemgetter

def my_filter(item):
    return item['type'] == 'type_1'

key = itemgetter('created_at')

items = sorted(filter(my_filter, data), key=key)
print(f"Num records is {len(items)}")
print(f"Earliest record is {key(items[0])}")

Num records is 2
Earliest record is 2020-02-12T17:45:00Z

Conversely you can use a generator-comprehension and then sort the generator.

gen = (item for item in data if item['type'] == 'type_1')
items = sorted(gen, key=key)
# rest of the steps are the same...
Sign up to request clarification or add additional context in comments.

Comments

1

You could use list comprehension to get all the sublists you're interested in, then sort by 'created_at'.

l = [{'id': 'id_123',
  'type': 'type_1',
  'created_at': '2020-02-12T17:45:00Z'},
 {'id': 'id_124',
  'type': 'type_2',
  'created_at': '2020-02-12T18:15:00Z'},
 {'id': 'id_125',
  'type': 'type_1',
  'created_at': '2020-02-13T19:43:00Z'},
 {'id': 'id_126',
  'type': 'type_3',
  'created_at': '2020-02-13T07:00:00Z'}]

ll = [x for x in l if x['type'] == 'type_1']
ll.sort(key=lambda k: k['created_at'])
print(len(ll))
print(ll[0]['created_at'])

Output:

2
02/12/2020 17:45:00

Comments

0

This is one approach using filter and min.

Ex:

data = [{'id': 'id_123',
  'type': 'type_1',
  'created_at': '2020-02-12T17:45:00Z'},
 {'id': 'id_124',
  'type': 'type_2',
  'created_at': '2020-02-12T18:15:00Z'},
 {'id': 'id_125',
  'type': 'type_1',
  'created_at': '2020-02-13T19:43:00Z'},
 {'id': 'id_126',
  'type': 'type_3',
  'created_at': '2020-02-13T07:00:00Z'}]

onlytype_1 = list(filter(lambda x: x['type'] == 'type_1', data))
print(len(onlytype_1))
print(min(onlytype_1, key=lambda x: x['created_at']))

Or:

temp = {}
for i in data:
    temp.setdefault(i['type'], []).append(i)

print(len(temp['type_1']))
print(min(temp['type_1'], key=lambda x: x['created_at']))

Output:

2
{'id': 'id_123', 'type': 'type_1', 'created_at': '2020-02-12T17:45:00Z'}

1 Comment

You have nice answers for the most part, but this constant use of list(filter(...)) and list(map(...)) with lambda is doing my head in. In those cases, a list comprehension is usually shorter, always more readable, and just as Pythonic, if not more =)
0

You can just generate a list of all the type_1s using a list_comprehension, and them use sort with datetime.strptime to sort the values accordingly

from datetime import datetime

# Generate a list with only the type_1s' created_at values
type1s = [val['created_at'] for val in vals if val['type']=="type_1"]

# Sort them based on the timestamps
type1s.sort(key=lambda date: datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ"))

# Print the lowest value
print(type1s[0])

#'2020-02-12T17:45:00Z'

Comments

0

You can use the following function to get the desired output:

from datetime import datetime

def sol(l):
    sum_=0
    dict_={}

    for x in l:
        if x['type']=='type_1':
            sum_+=1
            dict_[x['id']]=datetime.strptime(x['created_at'], "%Y-%m-%dT%H:%M:%SZ")

    date =sorted(dict_.values())[0]
    for key,value in dict_.items():
        if value== date: id_=key
    return sum_,date,id_

sol(l)

This function gives the number of times type ='type_1', corresponding minimum date and its id respectively.

Hope this helps!

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.