0

Is there a way to sort a lot of lists without having to write:

list.sort(list1)
list.sort(list2)
list.sort(list3)
...

for every single list? It is very tedious when you have a lot of lists

Theo
  • 55
  • 3
  • why dont you add all list content first in one list and then sort that final list – virendrao Oct 24 '16 at 07:46
  • Why do you have so many lists referenced by individual variables? Are they part of some bigger data structure? – Blender Oct 24 '16 at 07:47
  • I think your problem is to name the lists. No prob to build any loop around a list, a dictionary or any class. – am2 Oct 24 '16 at 07:49

2 Answers2

3

Probably best to use a for-loop:

lists = [1, 2, -1], [2, 0, 6], [91, 3, 82]    
for l in lists: l.sort()

list1, list2, list3 = lists

Now each list is sorted accordingly.

You could of course map it, but that's overkill since you'll also need to expand it into a list with list and dump away the resulting Nones produced as a side-effect:

_ = list(map(list.sort, lists))
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
2

you don't even Combine your lists to a named variable, so the code takes only 2 lines

l1 = [6,5,4,3,2,1]
l2 = [16,9,4,1]

#start of code
for my_l in [l1,l2]:
    list.sort(my_l)
#stop of code

print l1
print l2
am2
  • 380
  • 5
  • 21