Skip to content
Advertisement

Get latest timestamp from list of dictionaries

I have list of dictionaries that I am pulling from a ticketing system. Each dictionary contains the name and timestamp of a ticket.

There are cases where multiple tickets are entered for the same user and I would like to filter this list to only append the ‘latest’ timestamp to the list, rather than all occurrences.

Edit: I am looking to get a list of dictionaries returned that includes a list of all unique Name values with the largest Date value.

I have included updated list examples that might make it easier to work with.

My function that gathers the data is:

def get_onboarded_users():
    # The ticket that it is retrieving looks something like this:
    # "(IT) - Onboarding Initiated - Bill Bob"
    print("Collecting Onboarded Users", end="")
    url = 'https://********************/api/v3/requests'
    headers = {"authtoken": "*********************************"}
    rtn = []
    input_data = '''{
        "list_info": {
            "row_count": 5000,
            "start_index": 1,
            "sort_field": "subject",
            "sort_order": "asc",
            "get_total_count": true,
            "search_fields": {
                "subject": "(IT) - Onboarding Initiated"
            }
        }
    }'''
    params = {'input_data': input_data}
    response = requests.get(url, headers=headers, params=params)
    i = json.loads(response.text)
    user_requests = i['requests']
    onboarded_users = {}
    for user_request in user_requests:
        subject = user_request['subject'].upper()
        create_date = req['created_time']['value']
        user = subject.split(' - ')
        onboarded_users['Name'] = user[2]
        onboarded_users['Date'] = int(create_date) / 1000
        rtn.append(onboarded_users.copy())
    print(" - Complete")
    return rtn

My API call returns something that looks like this:

[
    { "Name": "Rob Smith", "Date": "1" },
    { "Name": "Rob Smith", "Date": "2" },
    { "Name": "Rob Smith", "Date": "3" },
    { "Name": "Bill Bob", "Date": "4" },
    { "Name": "Bill Bob", "Date": "7" },
    { "Name": "Sam Jackson", "Date": "1" }
]

and would like it to look like this:

[
    { "Name": "Rob Smith", "Date": "3" },
    { "Name": "Bill Bob", "Date": "7" },
    { "Name": "Sam Jackson", "Date": "1" }
]

Advertisement

Answer

You can use itertools.groupby.

import itertools

lst = [
    { "Name": "Rob Smith", "Date": "1" },
    { "Name": "Rob Smith", "Date": "2" },
    { "Name": "Rob Smith", "Date": "3" },
    { "Name": "Bill Bob", "Date": "4" },
    { "Name": "Bill Bob", "Date": "7" },
    { "Name": "Sam Jackson", "Date": "1" }
]

res = []
for key, group in itertools.groupby(lst, lambda x: x["Name"]):
    res.append(max(group, key= lambda y: y['Date']))
    
print(res)

Output:

[
    {'Name': 'Rob Smith', 'Date': '3'}, 
    {'Name': 'Bill Bob', 'Date': '7'}, 
    {'Name': 'Sam Jackson', 'Date': '1'}
]

As an alternative, You can use .

import pandas as pd
df = pd.DataFrame(lst)
res = df.groupby('Name')['Date'].max().reset_index().to_dict('records')
print(res)


# [{'Name': 'Bill Bob', 'Date': '7'},
#  {'Name': 'Rob Smith', 'Date': '3'},
#  {'Name': 'Sam Jackson', 'Date': '1'}]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement