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>Dictionaries <em>can't</em> have duplicates:</p>

<pre><code>some_data = [('foo', 'bar'),
             ('bang', 'quux'),
             ('foo', 'bar'),
             ('zappo', 'whoo'),
             ]

mydict = {}
for data in some_data:
    mydict[data[0]] = data[1]

import pprint; print(mydict)
</code></pre>

<p>The only way you're going to re-write the same data is if you aren't opening your file in <code>'w'</code> mode. But</p>

<pre><code>with open('outfile.txt', 'w') as of:
    for key in mydict:
        of.write('{},{}{}'.format(key, mydict[key], (',' + f + '\n') if verbose else '\n'))
</code></pre>

<p>Will never write the same line twice.</p>

python scapy