 This month I'm going to talk to you about Collections. I'm shocked. I haven't talked about this before earlier because it is really a great package It's part of a standard lib. I'm using Python 3.3 So there's a couple of things, but I'll point out that are only available in 3.3 actually So start let's just Get our stuff from collections. Let me talk about the weirdest thing in collections I've never really used an anger something called a DQ It's a fast in-memory list for certain operations if you want to add stuff to the left or to the right So, you know if you're talking about millions of things you're adding at one time This does very fast memory operations for insertions before and afterwards as opposed to the regular Python list Which is a bit slower. There's special methods and stuff You'll know when to use it when things are really slow and it gets really awkward Otherwise use regular lists The rest of stuff however is really cool Let me talk about One of my favorites default dick the default dick Default dick it's a bit hard to explain what it does is takes one arguments called a constructor in the docs What it is is a function that Whenever it's like a dick so it smells like a dick talks like a dick sounds like a dick Except that whenever it has a key error instead of raising a key error whenever it cannot find a key it will It will pass the key through The constructor function that you're passing through right So actually sorry it will call the constructor function and return that and set that so if you ever used a set default on a Dictionary it's sort of like that But does it automatically from the get-go with your with the function you pass in the default dick int in Python is a function If you call int with no arguments you get zero right so which can so what you can do is D Jan D Jan for example for January say plus equal one So let's try to look for D Jan cannot find it because D is empty because that's how that's how we built it It'll set it to zero automatically because I call it with int and add one to it And if I call say something else it'll be zero and If I sort all the items I get February in January one because January was zero and I added one above it I Imagine you've all done sort of let's say if you have to sort out plenty of dates. Let me see what was around Yeah, you know just dates around the calendar have I missed one did I put any in bad months? No, I don't think so Let's say I wanted a group and buy a year right so I call my default dick. I call the list again a List if you just call list will give you an empty list So let me go through my did my dates and by year n zero so n zero being the year I append my n and I can append because there's no care because it turns into list every item and suddenly I Get my year by items and they're all sorted by year 2011 2012 You've all done this code the hard time the hard way So a default is a really fun especially can pass in your own function So you have some really crazy things you want to do just pass a function takes no arguments return what you want And as opposed to set default where your second argument is going to get instantiated Functions only get called when you're missing something so if you want to call a function That's very expensive, but not all the time you can use it Also counter counters really great counter is a specialized Version of I guess the default ticked where it's a default ticked with my end here We're basically everything's equal to zero off the bat. So in my counter. I pass any iterable and And And I can add to that if I want to so again collections as a string is an iterable It's getting every letter at the time. So I just say see that letter add one And I can add like find me the five most common things right? This is really frequent in my code find me give me a huge list of stuff Get me the most common the three most common the top five the top 10 and I'll spit you out plus the count for each of those Right, so these are all the most common letters in the two strings I passed in one by one You know otherwise the way you're doing it normally in regular code is I'll get a dict and then go through my thing Right, and then if n is in counter counter n plus one else counter n equals one and you always screw it up as I always do it took me a couple of times to do this and Yeah, it's just a pain. Just use counters. It's great And Oh, yeah, also to get the most common. It's really complicated kind of code that I always screw up when I write by hand So the most common method is superb Now my favorite part of library. I've got 18 seconds. I'll do it really fast name tuple name tuples really great Use it. Please please even if I get cut off We're named tuple. It's like a tuple smells like a tuple. So this is a tuple use it everywhere you use a tuple and You can print it and it gets all these so all of these are fields speakers length and title and they become fields here and you can use them exactly like you would with a tuple and The cool thing is you can you can access things by attributes So in your code instead of doing a p0 everywhere you can do p dot speaker and I am done, but please use name tuple