 <p>There are a few opportunities for speedup, but my first concern is <em>vector</em>. Where is it initialized? In the code posted, it gets n^2 entries and sorted n times! That seems unintentional. Should it be cleared? Should final be outside the loop?</p> <p>final=sorted(vector, key=lambda vector: vector[2],reverse = True)</p> <p>is functional, but has ugly scoping, better is:</p> <p>final=sorted(vector, key=lambda entry: entry[2], reverse=True)</p> <p>In general, to solve timing issues consider using a <a href="https://docs.python.org/2/library/profile.html" rel="nofollow">profiler</a>. </p>