 <p>Your code is neat but the time complexity is O(n^2), which can be reduced to O(n).</p> <pre><code>data = {(1,2,1,5),(1,2,7,2),(1,5,4,7),(4,7,7,5)} result = dict() for item in data: key = (item[0],item[1]) value = result.setdefault(key,[]) value.append((item[2],item[3])) result[key] = value print result </code></pre> <p>In my opinion, using a for loop can make codes more comprehensive</p>