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.

Set comprehensions

Set comprehensions - Python Tutorial

From the course: Advanced Python

Start my 1-month free trial

Set comprehensions

- [Teacher] Let's wrap up our discussion on comprehensions by taking a look at how you can use comprehensions to work with sets of values. So, we've already seen how to do them with lists and dictionaries, and now we're going to take a look at sets. And recall, sets in Python are used to contain unique values. So that is each value in a given set can occur only once. So here in VS Code in my comprehensions folder I'll open up setcomp_start. And similar to our previous example, I have a list of Celsius temperature readings here, and what I want my code to do is convert the Celsius temperatures into Fahrenheit ones, but only keep each unique temperature reading. So in other words, I want to weed out any duplicate temperatures. In doing this with a set comprehension is pretty straight forward. So what I'm gonna do here for illustration purposes is solve the problem with both a list and a set comprehension, so you can see the difference. So, first what I'll do is build the comprehension…

Contents