Course 3.1Lesson 3 of 5
sorted gives a new sorted list and sort changes the list itself and gives back None. reverse equals True puts the biggest first, and key changes how the items are compared, for example key equals len for the length, or key equals str.lower to ignore capital letters. Tuples are compared item by item, so a list of pairs sorts by the first item, and a key function can sort by the second or by two things at once. Python's sort is stable, so equal items keep their old order. Bubble sort and selection sort are two simple methods that you can write yourself: they are slow for long lists, but easy to understand.
Try the code and the simulations beside the lesson.
After this lesson you can
sorted and sort, and change the order with reverse and key,Python can sort for you.
Predict first. Look at sorted(names). Where do you expect asha? Then run it.
sorted(x) makes a new sorted list. x.sort() sorts x itself and gives back None. Writing numbers = numbers.sort() loses the list.reverse=True puts the biggest first.key=len sorts by the length of each item. The key is a function that is called for each item, and the items are sorted by what it gives.key=str.lower sorts as if all the letters were small. Without it, asha comes last, because in the computer's order all the capital letters come before all the small letters.Often a list holds pairs, such as a name and a mark. Tuples are compared item by item, from the first. So sorted(students) sorts by the name, and for two equal names, by the mark.
You can sort by hand with a simple idea: compare neighbours, and swap them if they are in the wrong order. Repeat until no swap is needed.
The swap numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i] uses the tuple trick that you learned: no third variable is needed.
After 3 passes, 4 items are always in order. In real programs, use sorted(): it is much faster than bubble sort.
Here is a different idea. Find the smallest item of the whole list, and put it first. Then find the smallest of the rest, and put it second. And so on.
Bubble sort and selection sort are easy to understand, and that is their use: they show how sorting works. For 1000 items, they need about half a million comparisons. Python's own sorted uses a much faster method, and it is written in a faster language. In a real program, always use sorted. In the next lesson, you count the work to see the difference.
a = a.sort(). The list becomes None.TypeError: Python cannot compare 5 with "apple".key=str.lower.IndexError at i + 1.sorted(a) on its own line does nothing visible.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
sorted(x) is a new list. x.sort() changes x and gives back None.reverse=True and key= change how the list is sorted. A tuple key sorts by two things.sorted is faster than both.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
sorted([3, 1, 2], reverse=True) give?asha come last in sorted(["Meera", "asha", "Kabir"])?[5, 2, 9, 1], which value is in its place?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.