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:
JavaScript
x
34
34
1
def get_onboarded_users():
2
# The ticket that it is retrieving looks something like this:
3
# "(IT) - Onboarding Initiated - Bill Bob"
4
print("Collecting Onboarded Users", end="")
5
url = 'https://********************/api/v3/requests'
6
headers = {"authtoken": "*********************************"}
7
rtn = []
8
input_data = '''{
9
"list_info": {
10
"row_count": 5000,
11
"start_index": 1,
12
"sort_field": "subject",
13
"sort_order": "asc",
14
"get_total_count": true,
15
"search_fields": {
16
"subject": "(IT) - Onboarding Initiated"
17
}
18
}
19
}'''
20
params = {'input_data': input_data}
21
response = requests.get(url, headers=headers, params=params)
22
i = json.loads(response.text)
23
user_requests = i['requests']
24
onboarded_users = {}
25
for user_request in user_requests:
26
subject = user_request['subject'].upper()
27
create_date = req['created_time']['value']
28
user = subject.split(' - ')
29
onboarded_users['Name'] = user[2]
30
onboarded_users['Date'] = int(create_date) / 1000
31
rtn.append(onboarded_users.copy())
32
print(" - Complete")
33
return rtn
34
My API call returns something that looks like this:
JavaScript
1
9
1
[
2
{ "Name": "Rob Smith", "Date": "1" },
3
{ "Name": "Rob Smith", "Date": "2" },
4
{ "Name": "Rob Smith", "Date": "3" },
5
{ "Name": "Bill Bob", "Date": "4" },
6
{ "Name": "Bill Bob", "Date": "7" },
7
{ "Name": "Sam Jackson", "Date": "1" }
8
]
9
and would like it to look like this:
JavaScript
1
6
1
[
2
{ "Name": "Rob Smith", "Date": "3" },
3
{ "Name": "Bill Bob", "Date": "7" },
4
{ "Name": "Sam Jackson", "Date": "1" }
5
]
6
Advertisement
Answer
You can use itertools.groupby
.
JavaScript
1
17
17
1
import itertools
2
3
lst = [
4
{ "Name": "Rob Smith", "Date": "1" },
5
{ "Name": "Rob Smith", "Date": "2" },
6
{ "Name": "Rob Smith", "Date": "3" },
7
{ "Name": "Bill Bob", "Date": "4" },
8
{ "Name": "Bill Bob", "Date": "7" },
9
{ "Name": "Sam Jackson", "Date": "1" }
10
]
11
12
res = []
13
for key, group in itertools.groupby(lst, lambda x: x["Name"]):
14
res.append(max(group, key= lambda y: y['Date']))
15
16
print(res)
17
Output:
JavaScript
1
6
1
[
2
{'Name': 'Rob Smith', 'Date': '3'},
3
{'Name': 'Bill Bob', 'Date': '7'},
4
{'Name': 'Sam Jackson', 'Date': '1'}
5
]
6
As an alternative, You can use pandas.
JavaScript
1
10
10
1
import pandas as pd
2
df = pd.DataFrame(lst)
3
res = df.groupby('Name')['Date'].max().reset_index().to_dict('records')
4
print(res)
5
6
7
# [{'Name': 'Bill Bob', 'Date': '7'},
8
# {'Name': 'Rob Smith', 'Date': '3'},
9
# {'Name': 'Sam Jackson', 'Date': '1'}]
10