Merge counter.items() dictionaries into one dictionary
<p>How do I get the output of this code into one dictionary with total number of key:value pairs?</p>

<pre><code>import re
from collections import Counter

splitfirst = open('output.txt', 'r')
input = splitfirst.read()
output = re.split('\n', input)

for line in output:
    counter = Counter(line)
    ab = counter.items() #gives list of tuples will be converted to dict
    abdict = dict(ab)
    print abdict
</code></pre>

<p>Here is a sample of what I get:</p>

<pre><code>{' ': 393, '-': 5, ',': 1, '.': 1}
{' ': 382, '-': 4, ',': 5, '/': 1, '.': 5, '|': 1, '_': 1, '~': 1}
{' ': 394, '-': 1, ',': 2, '.': 3}
{'!': 1, ' ': 386, 'c': 1, '-': 1, ',': 3, '.': 3, 'v': 1, '=': 1, '\\': 1, '_': 1, '~': 1}
{'!': 3, ' ': 379, 'c': 1, 'e': 1, 'g': 1, ')': 1, 'j': 1, '-': 3, ',': 2, '.': 1, 't': 1, 'z': 2, ']': 1, '\\': 1, '_': 2}
</code></pre>

<p>I have 400 of such dictionaries, and Ideally I have to merge them together, but if I understand correctly Counter does not give them all, rather gives them all one after another.</p>

<p>Any help would be appreciated. </p>

<p>Here's a <a href="http://stackoverflow.com/help/mcve">mcve</a> of your problem:</p>

<pre><code>import re
from collections import Counter

data = """this is the first line
this is the second one
this is the last one
"""

output = Counter()
for line in re.split('\n', data):
    output += Counter(line)

print output
</code></pre>

<p>Applying the method in your example you'd get this:</p>

<pre><code>import re
from collections import Counter

with open('output.txt', 'r') as f:
    data = f.read()

    output = Counter()
    for line in re.split('\n', data):
        output += Counter(line)

    print output
</code></pre>

python python-2.7 dictionary merge counter