'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>It looks like your problem is the way you add the tuple to the list. Here is an example to show the problem :</p>

<pre><code>l = []
l += ("a", "b")
print l

l = []
l.append( ("a", "b"))
print l  
</code></pre>

<p>Which gives :</p>

<pre><code>&gt;&gt;&gt; ['a', 'b']
&gt;&gt;&gt; [('a', 'b')]
</code></pre>

<p>So <code>list+=tuple</code> is equivalent to calling <code>list.extend(tuple)</code> and not <code>list.append(tuple)</code> which is what you want.</p>

<p>A side note on the meaning of the exception that was raised :
<code>X is not subscriptable</code> means that your are trying to call that syntax <code>X[some int]</code> while the object doesn't support it.</p>

python python-2.7 datetime tuples