Python: Dedup a dictionary
<p>Good afternoon,</p>

<p>I reading in a pcap and am basically trying to get a dedup'd list of BSSID's &amp; ESSID's.  I am still getting duplicates with this code and cannot for the life of me figure out what I am doing wrong:</p>

<pre><code>if not (t[0] in ssid_pairs and ssid_pairs[t[0]] == t[1]):
    ssid_pairs[t[0]] = t[1]
    of.write(t[0] + ',' + t[1] + ((',' + f + '\n') if verbose else '\n'))
</code></pre>

<p>ssid_pairs is a dictionary, t[0] is the bssid &amp; t[1] is the essid.  An example of the dictionary is:</p>

<pre><code>{'FF:FF:FF:FF:FF:FF':'MyWIFI',...}
</code></pre>

<p>I am still seeing multiple instances of the same key->value pair being written to the file.  I put some debugging print statements in and sometimes it will recognize a duplicate, sometimes it will not.  This is from a parse pcap with scapy.</p>

<p>Thanks for any help.</p>

<p>*** EDIT: Thanks everyone, I am not really solving my problem the right way with a dictionary.  Time to think this through a bit clearer...</p>

<p>Let's say you get:</p>

<pre><code>t[0] = 'foo'
t[1] = 'bar'
</code></pre>

<p>Then we hit your code above:</p>

<pre><code>if not (t[0] in ssid_pairs and ssid_pairs[t[0]] == t[1]):
    ssid_pairs[t[0]] = t[1]
    of.write(t[0] + ',' + t[1] + ((',' + f + '\n') if verbose else '\n'))
</code></pre>

<p>The condition passes (because <code>t[0]</code> is not in <code>ssid_pairs</code>), so we set:</p>

<pre><code>ssid_pairs[t[0]] = t[1]
</code></pre>

<p>Which gives us:</p>

<pre><code>ssid_pairs = {
  'foo': 'bar',
}
</code></pre>

<p>In our next iteration of the loop, we read:</p>

<pre><code>t[0] = 'foo'
t[1] = 'gizmo'
</code></pre>

<p>Your condition passes (because <code>ssid_pairs[t[0]] != t[1]</code>), so we set:</p>

<pre><code>ssid_pairs[t[0]] = t[1]
</code></pre>

<p>Which gives us:</p>

<pre><code>ssid_pairs = {
  'foo': 'gizmo',
}
</code></pre>

<p>Then we read the same data we encountered in our first iteration:</p>

<pre><code>t[0] = 'foo'
t[1] = 'bar'
</code></pre>

<p>But because we just <code>ssid_pairs['foo'] = 'gizmo'</code>, your condition will pass again, and you  will once again write out the same data.</p>

<p>If you are trying to get a list of unique <em>pairs</em>, then maybe create a set of <code>(essid,bssid)</code> tuples:</p>

<pre><code>seen_pairs = set()
...
if not ((t[0],t(1)) in seen_pairs):
    seen_pairs.add((t[0], t[1]))
    of.write(t[0] + ',' + t[1] + ((',' + f + '\n') if verbose else '\n'))
</code></pre>

python scapy