Check out a free preview of the full Python Fundamentals course

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

Nina reviews what was covered in the previous sections.

Preview
Close

Transcript from the "Set Review" Lesson

[00:00:00]
>> Nina Zakharenko: Going back to the top here, remember you have some of these set operations in a table. So,
>> Nina Zakharenko: This whole course, there's a lot of information, and the way that you'll retain it is just with muscle memory, typing in the code, doing examples, sticking with it. If there's a particular thing that you're not using frequently, you'll probably have to look it up, and that's okay, that's why the course website is here.

[00:00:31]
I expect nobody to absorb every single piece of information that I'm gonna tell you today. So okay, set cheat sheet. Sets are used for storing immutable data types uniquely. It's easy to,
>> Nina Zakharenko: It's a bit of a typo. It's easy to find out if an item is in a set or not, it's a very fast operation.

[00:01:00]
To create sets, remember, what kind of data structure do we see if try to make an empty set with just two empty squiggly brackets, curly brackets?
>> Student: Dictionary?
>> Nina Zakharenko: Dictionary, right, so to create an empty set, use the set method. And you can use the curly braces with items separated by commas to create a set with items in it.

[00:01:23]
Search method, check to see if the item is in the set with the incurred. Search speed, searching for an item in a large set is very fast, cuz it uses that hash that I showed you under the hood. Some common set methods Set dot. My set.add item, my set.discard item, to remove the item if it's present, and not throw an error if it's not.

[00:01:50]
My set.remove item to remove the item if it's present, but throw an exception if it's not. Or my set.update other set. And remember just like with lists, you don't want to name your sets set cuz you'd be overriding a built in type. Is the order preserved?
>> Student2: No.

[00:02:11]
>> Nina Zakharenko: Right, can you access items in a set by index? No. Is a set mutable?
>> Student2: Yes.
>> Nina Zakharenko: It is, right?
>> Student2: You said immutable?
>> Nina Zakharenko: Mutable, is the set mutable?
>> Student3: Yes, it's mutable.
>> Nina Zakharenko: Yes, you can add and remove items from the set. Can you sort a set in place?

[00:02:33]
>> Class: No.
>> Nina Zakharenko: No, cuz the items are in order right?
>> Nina Zakharenko: What kind of data structure can you sort?
>> Class: A list.
>> Nina Zakharenko: So one quick kind of hack here, if you wanted to sort the items in a set, and you know that a list is sortable, what might you want to do in this situation?

[00:03:00]
>> Student3: Could you move the set into a list?
>> Nina Zakharenko: Right, and we saw this earlier, but we can call the list constructor on our set. And all of a sudden, we have a copy back, right? So our original rainbow color set is still there, this is just a copy that's now a list.

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