2

How can I reorder a list in python according to another list, but without creating a new list, since it is linked by several objects?

This question and it's answers create new lists, so they don't apply.

Community
  • 1
  • 1
hdrz
  • 461
  • 6
  • 12
  • Use [slice assignment](http://stackoverflow.com/questions/10623302/how-assignment-works-with-python-list-slice). – Ashwini Chaudhary Feb 18 '14 at 10:37
  • @AshwiniChaudhary - That will work fine for a list of integers (as the slice), but what if I have something different? – hdrz Feb 18 '14 at 10:41

1 Answers1

3

You can create the list however you want, and then reassign the elements. For example:

 X[:] = [x for (y,x) in sorted(zip(Y,X))]
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132