In [5]: sorted(unsorted_list, key=presorted_list.index) Out[5]: ['2C', '2D', '3D', '4D']
or, for better performance (especially when len(presorted_list) large)
In [6]: order = {item:i for i, item in enumerate(presorted_list)} In [7]: sorted(unsorted_list, key=order.__getitem__) Out[7]: ['2C', '2D', '3D', '4D']
For more information on how to sort with key s, see the excellent Howto Sort wiki .
If unsorted_list contains elements (for example, '6D' ) not in presorted_list , then the above methods will cause an error. First you need to decide how you want to sort these items. If you want them to be placed at the end of the list, you can use
In [10]: unsorted_list = ['3D','2C','6D','4D','2D'] In [11]: sorted(unsorted_list, key=lambda x: order.get(x, float('inf'))) Out[11]: ['2C', '2D', '3D', '4D', '6D']
or if you want to place such items at the top of the list, use
In [12]: sorted(unsorted_list, key=lambda x: order.get(x, -1)) Out[12]: ['6D', '2C', '2D', '3D', '4D']