Python always works by reference, unless you explicitly ask for a copy (a slice of a built-in list is deemed to "ask for a copy" -- but a slice of a numpy array also works by reference). However, exactly because of that, alist=anotherlist; alist.sort()
means the single list objects (with two equivalent names alist
and anotherlist
) gets sorted -- you can't maintain two different orderings at the same time on the same list object.
So, in this case, you must explicitly ask for a copy (e.g. alist=list(anotherlist)
) -- and once you've done that there is no more connection between the two distinct list objects. You can't have it both ways: either you work by reference (and have a single list object and thus a single ordering!), or you make a copy (in which case you end up with two separate list objects).
You could take advantage of the fact that the copies discussed so far are shallow -- the objects (items) that the two lists refer to are the same... until and unless you perform removals, additions, or reassignments of items on either list (mutation of mutable items on the other hand don't alter this connection: it's a completely separate and drastically different situation from any of the above, since removals, additions and reassignments are operation on the list, while calling a mutating method on an item is an operation on the item -- items are oblivious to any operation on one or more lists referring to them, lists are oblivious to any operation on one or more of the items they refer to).
There's not much you can do about removals and additions, except keeping two lists wrapped and synced up in a single object as suggested in other answers; but for reassignments of items, if that's all you require, you could turn those into mutation of items by adding one level of indirection -- instead of having a list directly referring to the items, have it refer e.g. to one-item sublists. For example:
>>> alist = list([x] for x in 'ciao')
>>> blist = list(alist)
>>> blist.sort()
>>> alist
[['c'], ['i'], ['a'], ['o']]
>>> blist
[['a'], ['c'], ['i'], ['o']]
>>> blist[-1][0] = 'z'
>>> blist
[['a'], ['c'], ['i'], ['z']]
>>> alist
[['c'], ['i'], ['a'], ['z']]
Whether this concept of an extra indirection level can help at all with what you're exactly trying to do, only you can tell, since we don't really know what it is that you are trying to do;-).