'datetime.datetime' object is not subscriptable
<p>I've checked every other post about this, but none can fix my issue.</p>

<p>I made a list that holds tuples with an id and a datetime object. Everytime I try to clean up the list with:
<code>last_encounters = [item for item in last_encounters if item[1] &lt; datetime.utcnow]</code>
I get the error that <code>'datetime.datetime' object is not subscriptable</code>. It's getting pretty annoying, I tried dicts.. didn't work.</p>

<p>Also tested the item[1], according to my print it is a datetime.</p>

<p>Even tried changing it to <code>(x,y) for x,y in last_encounters if y &lt; ...</code> also did NOT work.</p>

<p>Some useful code:</p>

<pre><code>list = []
d_t = datetime.utcfromtimestamp(9000000)     
list += [('lel', d_t)]     
list = [item for item in list if item[1] &lt; datetime.utcnow]
</code></pre>

<p>I hope someone can tell me what I am doing wrong here.</p>

<p>Thanks in advance,</p>

<p>Kevin</p>

<p>When you do <code>last_encounters += (a, b)</code>, you are adding two sequences together, <code>last_encounters</code> and <code>(a,b)</code>.  This means you end up with <code>a</code> and <code>b</code> stuck on the end of the list, rather than just adding the tuple to the list.</p>

<p>There are two options to fix your problem:</p>

<ol>
<li><p>Add a sequence containing your tuple:</p>

<pre><code> last_encounters += [(d["id"], d["d_t"])]
</code></pre></li>
<li><p>Or preferably, use the <code>append</code> method:</p>

<pre><code> last_encounters.append((d["id"], d["d_t"]))
</code></pre></li>
</ol>

python python-2.7 datetime tuples