Python Fundamentals

Changing the Order of a List

Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "Changing the Order of a List" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces methods to change the order of lists, given their contents, then goes on to view all the methods available to lists, and how to see what a particular method does from the REPL.


Transcript from the "Changing the Order of a List" Lesson

>> Nina Zakharenko: Because lists have an order we can access them by index, that also means we can sort them. So let's say that I have some lottery numbers, in a list. And you guys can make up your own, it's your lucky day, so [LAUGH]
>> Nina Zakharenko: These are my lottery numbers, right?

There are two ways of sorting in Python. Let me clear the screen and kind of move this back to the top, there's two ways of sorting.
>> Nina Zakharenko: The first way is to,
>> Nina Zakharenko: Take that original list and then return a copy of the list that's sorted, but leave the lists in that variable completely alone.

And just like with LAN, for length, sorted as a built-in in Python. So we can call sorted and pass in the lottery numbers, and look at that, they are sorted, right? But if we look at our underlying variable, it's still exactly the same, just like we declared it.

>> Nina Zakharenko: Sorted takes an optional argument, and by the way, to scroll back and forth in my history, I'm just using the up arrows and the down arrows, it might save you some time. So remember these optional default arguments.
>> Nina Zakharenko: So by default, the list is sorted in increasing order.

But if I pass in this argument to sorted reverse equals true, it's gonna sort my list backwards. And again, looking at the list, the original list remains unchanged, right? Notice that calling sorted returns something, right, it returns a copy. So, I can save this to a variable, and all of a sudden in X I have my new sorted list of saved back copy.

Okay, second way of sorting list, is to do it in place, so this operation modifies the underlying list, your data is going to change. And one of the hints that that is happening is, if I have my lottery numbers, I can call .sort on it. And notice that I'm calling this method on my own variable, right?

And when I call this method nothing returns, right. Or technically, it's the non type that's returned, but we're not getting a new copy of our list because there is no copy. So if we look in lottery numbers now, we'll see that our lottery numbers have been sorted in place, the data structure has been modified, right.

So a little bit of trickery, you would think lottery numbers dot sort reverse sequel true, unfortunately, not quite that easy, so there’s just an extra method to remember here. And that would be lottery_numbers.reverse. Again it returns nothing, why is that happening?
>> Student: Doing it in place.
>> Nina Zakharenko: Because it's doing it in place, right.

If I look at my lottery numbers, they're now sorted backwards
>> Nina Zakharenko: Remember, if you forget all of this stuff you can call, because if we look at the type of lottery numbers,
>> Nina Zakharenko: it's class list, right? So, if I wanted to see the directory listing of all of the methods available on list, what would I call?

>> Class: Dir.
>> Nina Zakharenko: Dir on list, right? Okay, and the ones without the underscores are generally the ones that we care about. I used append a little bit earlier, right? Here's reverse sort, if I didn't remember what arguments it took, what would I type into my REPL?
>> Class: Help.

>> Nina Zakharenko: Help, right.
>> Student2: List.
>> Nina Zakharenko: List.
>> Student2: And then .reverse.
>> Nina Zakharenko: .reverse, right, yeah. No extra parentheses here or anything, just the type and the method, and we'll see that pretty helpful method comment, right? It reverses the list in place, cool.
>> Nina Zakharenko: So taking a quick look at the cheat sheet here, what's the use for a list?

It's used for storing similar items in cases where items need to be added or removed. You can create the list with the square brackets, or list for an empty list, or for a list with items. You could just put the square brackets and put some information in there, right, variables separated by comma.

>> Nina Zakharenko: Cool,
>> Nina Zakharenko: So we looked at checking the name, or I'm sorry checking the length of the list. We just do that with one.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now