 <p>Your code only keeps the first encountered value for a given key. You can use <code>defaultdict</code> from the <code>collections</code> module to initialize <code>result</code> with a fresh <code>dict</code> and then set the key to the value as encountered in your list of tuples.</p> <pre><code>#all_tuples = [(...), (...), ...] from collections import defaultdict result = defaultdict(dict) for uid, k, v in all_tuples : result[uid][k] = v dict(result) </code></pre> <p>Output:</p> <pre><code>{1: {1: 2, 3344: 1, 756735: 1}, 4: {23: 1}, 23: {4: 1, 534: 1, 899979879: 1}, 534: {23: 1}, 3344: {1: 1}, 9977: {980898: 1}, 756735: {1: 1, 980898: 1}, 980898: {9977: 1, 756735: 1, 98789797: 1}, 98789797: {980898: 1}, 899979879: {23: 1}} </code></pre>