Parsing DeepDiff result
<p>I am working with <a href="http://deepdiff.readthedocs.io/en/latest/" rel="nofollow">DeepDiff</a>. So I have results like:</p>

<pre><code>local =  [{1: {'age': 50, 'name': 'foo'}}, {2: {'age': 90, 'name': 'bar'}}, {3: {'age': 60, 'name': 'foobar'}}]
online = [{1: {'age': 50, 'name': 'foo'}}, {2: {'age': 40, 'name': 'bar'}}]
ddiff = DeepDiff(local, online)
added, updated = ddiff['iterable_item_added'], ddiff['values_changed']
added = {'root[2]': {3: {'age': 60, 'name': 'foobar'}}}
updated = {"root[1][2]['age']": {'new_value': 90, 'old_value': 40}}
</code></pre>

<p>Now, I want to take:</p>

<pre><code>list_indexes_added = foo(added)
list_indexes_updated = foo(updated)
</code></pre>

<p>and to obtain:</p>

<pre><code>list_indexes_added = [2]
list_index_updated = [(1,2,'age')]
</code></pre>

<p>in this way, I can manipulate the list <code>local</code> and <code>online</code> and in the future update the <code>online</code> table.</p>

<p>I am thinking in regexs, but maybe there is other option.</p>

<p>So, I'd go with something like this:</p>

<pre><code>import re

def foo(diff):
modded = []

for key in diff.keys():
    m = re.search('\[(.+)\]', key)
    modded.append(tuple(m.group(1).split('][')))

return modded
</code></pre>

<p>It'll read each key, extract only the indices (whether numeric or string), then slice up the string. Since your desired output indicates a tuple, it'll spit the sequence of indices back as one, then return the whole list of index sets (since <code>diff</code> might have more than one).</p>

<p>This can be golfed down into a one-line list comprehension:</p>

<pre><code>import re

def foo(diff):
    return [tuple(re.search('\[(.+)\]', key).group(1).split('][')) for key in diff.keys()]
</code></pre>

python regex algorithm diff