Hassan Baig
Hassan Baig

Reputation: 15824

Complex python list processing

I have a python list made up of usernames and time stamp tuples. Imagine it's like the following:

[(username,datetime_obj),(username,datetime_obj),(username,datetime_obj),(username,datetime_obj),(username,datetime_obj),(username,datetime_obj),(username,datetime_obj),(username,datetime_obj),(username,datetime_obj)]

Next, imagine the list above has only 3 unique usernames, but that all datetime objects are unique.

What's the most efficient, pythonic way to derive a new list from the one above, which is again made up of tuples and the same usernames, except that next to each username, the most recent datetime_obj in the list (for that particular username) is repeatedly attached.

E.g. if starting list was [(sam,1),(sam,7),(sam,8),(jon,4),(mel,9),(mel,2),(mel,10),(jon,3),(jon,6)], I end up with [(sam,1),(sam,1),(sam,1),(jon,3),(mel,2),(mel,2),(mel,2),(jon,3),(jon,3)].

I used ints to depict datetime objects in the example above. This was just for simplicity.

Thanks in advance.

Upvotes: 3

Views: 98

Answers (1)

L3viathan
L3viathan

Reputation: 27273

I think you can't get around iterating over the list twice:

most_recent = {}
for user, date in myList:
    most_recent[user] = max(most_recent.get(user, date), date)

newList = [(user, most_recent[user]) for user, _ in myList]

You can do something like this, if you consider this more pythonic, but it is slower (quadratic complexity), so don't actually do it:

[(user, max(date for u, date in myList if u == user)) for user, _ in myList]

Upvotes: 3

Related Questions