Intermediate Python

Sets & Dictionaries

Intermediate Python

Check out a free preview of the full Intermediate Python course

The "Sets & Dictionaries" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to create sets and dictionaries recursively.

Preview
Close

Transcript from the "Sets & Dictionaries" Lesson

[00:00:00]
>> Nina Zakharenko: We talked about four different data types yesterday.
>> Nina Zakharenko: Four different container,
>> Nina Zakharenko: Data types. We talked about lists, tuples, sets, dictionaries, right? Do you think that these other container types have comprehensions? They do, yeah. So if I wanted to do a set comprehension, I would use, I remember that set is kind of those curly braces, right?

[00:00:39]
This is a dictionary. We remember that, but as long as there are items in it, then it's a set. So we're gonna use this curly brace syntax. Let's say we want to do a set of our squares. I would do the same thing that I did in my list comprehension.

[00:01:02]
Let's say for num in range,
>> Nina Zakharenko: Let's give it a bigger range now, for num in range(11). That's my loop and then my action is just num * num.
>> Nina Zakharenko: Remember, sets are unordered, so you won't see the results in order like you would in the list, right?

[00:01:26]
And to quickly check that, I can change the start on the end,
>> Nina Zakharenko: Right, and confirm my assumptions.
>> Nina Zakharenko: What's the difference in syntax between a set and a dictionary? Consider a set to be a collection of keys, just keys. What's different about a dictionary?
>> Speaker 2: It's values attached those keys.

[00:01:53]
>> Nina Zakharenko: Yes, key value pairs.
>> Nina Zakharenko: What's different between our syntax for a set and our syntax for a dictionary?
>> Speaker 2: Commas.
>> Nina Zakharenko: Colons.
>> Speaker 2: Commas.
>> Nina Zakharenko: It's not commas cuz you differentiate the items in your set by commas. But it-
>> Speaker 2: Colons.
>> Nina Zakharenko: It is colons, yeah. So what if I wanted to make a dictionary where the key was the original number and the value was the square?

[00:02:29]
So my key is the original number. The value is the square. So we know I have to put in a colon. Where does the key go?
>> Speaker 2: Before the value.
>> Nina Zakharenko: Right, before the value.
>> Nina Zakharenko: Now I've turned my set comprehension into a dictionary comprehension. Just like I showed you f strings before, you can use f strings in your comprehensions.

[00:02:56]
So let's say I had my list of names right here.
>> Nina Zakharenko: I can do a list or a set comprehension, I can do a dictionary comprehension. But just for simplicity, I'm gonna do a list comprehension and I'm gonna say for name in names. And as my expression, I can put an f string.

[00:03:24]
And say the name is {name}. Let's say I had a list of names with dups in it, I'm gonna call it dups_names. And there's gonna be Nina in here, and Max, and Nina again.
>> Nina Zakharenko: I know that I can call set on dups_names and it will take out the duplicates.

[00:03:51]
I can also call set on,
>> Nina Zakharenko: A list comprehension.
>> Nina Zakharenko: Let's say that I wanted to get the length of each name in this list. So I would say, for name in dups_names,
>> Nina Zakharenko: And I would print out. Sorry, the value would be the length of the name.

[00:04:19]
And so my set just has 3 and 4 in it because the name Nina has the same length twice.
>> Nina Zakharenko: Something to

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