Searching for Key, Value in a list of dictionaries
<p>Answer to the question:</p>

<p>After some help, I realized that it was breaking because it was scanning through the emails and while one email would have what I was looking for, the rest didn't, and thus caused it to break. </p>

<p>Adding in a Try/Except solved the problem. Just for historical sake incase anyone else looks for a similar problem, this is the code that worked.</p>

<pre><code>try:
  if (item for item in list_of_dict if item['name'] == "From" and item['value'] == 'NAME1 &lt;name@some_email.com&gt;').next():
    print('has it')
  else:
    pass
except StopIteration:
  print("Not found")
</code></pre>

<p>This way it would be able to scan through each email and have error handling if it broke, but if it found it be able to print that I found what I was looking for. </p>

<p>Original question:</p>

<p>Code:</p>

<p><code>if (item for item in list_of_dict if item['name'] == "From" and item['value'] == 'NAME1 &lt;name1@some_email.com&gt;').next()</code></p>

<p>I'm getting a <code>StopIteration</code> error:</p>

<pre><code>Traceback (most recent call last):
  File "quickstart1.py", line 232, in &lt;module&gt;
    main()
  File "quickstart1.py", line 194, in main
    if (item for item in list_of_dict if item['name'] == "From" and item['value'] == 'NAME1 &lt;name1@some_email.com&gt;').next():
StopIteration
</code></pre>

<p>This is my code:</p>

<pre><code>if (item for item in list_of_dict if item['name'] == "From" and item['value'] == 'NAME1 &lt;name1@some_email.com&gt;').next():
      print('has it')
else:
  print('doesnt have it')
</code></pre>

<p>When I checked to see if I am putting in the iterator incorrectly, I did a lookup for item['value']:</p>

<p><code>print((item for item in list_of_dict if item['name'] == "From").next())</code></p>

<p>Returns:</p>

<pre><code>{u'name': u'From', u'value': u'NAME1 &lt;name1@some_email.com&gt;'}
{u'name': u'From', u'value': u'NAME2 &lt;name2@some_email.com&gt;'}
</code></pre>