How to Sort Multiple Lists in Python
The list is the basic "ordered set" data type in Python, much akin to the array data type in other languages. Lists have a built-in "sort" method you can use to sort them, and the "sorted" function returns a sorted version of the list without affecting the original. Lists can also be concatenated, so you can use "sorted" to sort multiple lists together in a single command without changing them.
Instructions
-
-
1
Run the Python command-line interpreter, and type the following commands to define a few lists:
list1 = [4,6,7]
list2 = [1,8,2]
list3 = [9,3,5]
-
2
Sort the lists together and store the result in a new list using the following command:
list4 = sorted(list1 + list2 + list3)
Note the use of the concatenation operator, "+," to combine the lists together.
-
-
3
Type the command "list4." The list's contents are displayed, consisting of all the elements of the other lists sorted together. Type the names of the other lists to see that they are not affected.
-
1
- Photo Credit Thinkstock/Comstock/Getty Images