Python Query list for oldest date
<p>My query returns the following in a list:</p>

<pre><code>"Alex";"275467125";"2015-02-03 02:55:36-05";"1"
"Alex";"275467125";"2015-01-13 02:09:39-05";"1"
"Alex";"275467125";"2015-01-05 04:13:35-05";"1"
"Alex";"275467125";"2014-12-27 04:55:47-05";"1"
"Alex";"275467125";"2014-12-27 04:54:52-05";"1"
"Alex";"275467125";"2014-12-07 03:13:24-05";"1"
"Alex";"275467125";"2014-12-04 03:34:56-05";"1"
"Alex";"275467125";"2014-12-02 04:16:33-05";"1"
"Ali";"275464747";"2016-02-17 10:52:12-05";"2"
"Alladin";"275467455";"2016-03-13 06:51:52-04";"2"
"Alladin";"275467455";"2016-03-13 06:51:47-04";"2"
"Anna";"275467401";"2016-03-26 03:56:41-04";"1"
"Anna";"275467401";"2016-03-26 03:55:21-04";"1"
"Anna";"275467401";"2016-03-21 23:04:28-04";"1"
"Anna";"275467401";"2016-02-12 13:24:44-05";"1"
"Anna";"275467401";"2015-12-03 08:20:35-05";"1"
"Anna";"275467401";"2015-11-09 04:18:27-05";"1"
"Anna";"275467401";"2015-11-09 04:11:59-05";"1"
"Anna";"275467401";"2015-09-13 21:27:12-04";"1"
</code></pre>

<p>I want to create a dictionary of person's name with the oldest record they have. I've figured out:</p>

<pre><code>oldestlist = {d[0]:d[2] for d in records}
</code></pre>

<p>This returns a correct answer but my worry is that if I am presented a list that is not formatted in a descending order of date/time it will not provide the correct answer. What is the best way to create a dictionary with a name and the oldest date?</p>

<p>I was pretty close. The answer I found that works best was a tweak of my original code but using the sorted() function. </p>

<p>For the newest I'd do:</p>

<pre><code>newestlist = {d[0]:d[2] for d in sorted(records)}
</code></pre>

<p>For the oldest I'd do:</p>

<pre><code>oldestlist = {d[0]:d[2] for d in sorted(records, reverse=True)}
</code></pre>

<p>Thanks to everyone who answered. I'll keep in mind the django references for if I use a queryset. </p>

python list date dictionary time