From the course: Advanced Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

OrderedDict

OrderedDict - Python Tutorial

From the course: Advanced Python

Start my 1-month free trial

OrderedDict

- [Instructor] One of the main downsides of the regular dictionary object in Python is that it doesn't keep track of any order among the items. The OrderedDict is a dictionary object that remembers the order in which items are inserted, and we'll take a look at that in this example now. So I'm going to open up the ordereddict_start file here in my Collections folder. And here in my code, I have a list of tuples, each of which represents a sports team, along with a tuple that contains their win and loss record. And I'm using the built-in sorted function to sort the list by the number of wins. And we saw this function earlier in the course. And you can see that the key function I'm using here is a lambda function, which is getting the first item in the embedded tuple here to sort by the number of wins. And you can also see that I'm using an OrderedDict. I'm importing OrderedDict here from the collections, from the Collections module. And what I'm going to do first is I'll use this list…

Contents