scott martin
scott martin

Reputation: 1293

Python - Extracting values from a nested list

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

Upvotes: 0

Views: 67

Answers (5)

jitesh2796
jitesh2796

Reputation: 304

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!

Upvotes: 0

jignatius
jignatius

Reputation: 6474

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

Upvotes: 1

fixatd
fixatd

Reputation: 1404

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'

Upvotes: 0

Rakesh
Rakesh

Reputation: 82755

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'}

Upvotes: 0

gold_cy
gold_cy

Reputation: 14216

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...

Upvotes: 1

Related Questions