[Tutor] Sorting more than one list

Alan Gauld alan.gauld at freenet.co.uk
Fri Apr 1 09:59:20 CEST 2005


> I need to sort 4 lists but I need that they make the "sort
together".
> I'll sort just one but when I change the position of the items of
the
> 1st list I have to change the positions of the other 3 lists. Can I
do
> this just using the sort() method of the list object?
> If I can't, someone know a site that have one sort method in python
that
> its easily to implement and fast?

Since the data are obviously related (since you need to keep them
linked),
I'd be inclined to merge the lists into a list of tuples

merged = [(a,b,c,d) for a in l1 for b in l2 for c in l3 for d in l4]

Then you can sort 'merged' and it should just work.... The default
sort will order the tuples by the first member...

Alan G.



More information about the Tutor mailing list