The first proposed solution β using a βlistβ to include ranges in lists β is inefficient, since it will first turn range objects into lists (potentially consuming a lot of memory if ranges are large), and then compare each item. Consider, for example, a = range (1,000,000), the βrangeβ object itself is tiny, but if you force it to a list, it becomes huge. Then you need to compare one million elements.
Answer (2) is even less effective, since assertItemsEqual is not only going to instantiate lists, but also sorts them before doing an elementary comparison.
Instead, since you know that objects are ranges, they are equal when their steps, start and end values ββare equal. For instance.
ranges_equal = len(a)==len(b) and (len(a)==0 or a[0]==b[0] and a[-1]==b[-1])
source share