Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Sets Set Basics

Could somebody explain the "Set Basics" Teacher's Notes to me?

I don't know why but I'm just not understanding this example: "One of the most common ways you'll see sets being used is to make some other iterable unique. For example, say you have a list of page numbers where terms appear in a book. Since some pages could contain multiple terms, you're likely to get repeats. In that case, you'll see people doing code like this:

pages = list(set(pages)) "

2 Answers

Try the code below:

Assume 'pages' variable below has the page numbers in a book where a search word appears.

The page numbers are repeated because the search term appears multiple times in the same page.

pages = 1,1,2,3,4,4,4,5,5,1,1

'set' will allow to remove the duplicate page numbers and return the unique ones.

the outer 'list' will just convert the set to list type.

list(set(pages))

Output will be [1, 2, 3, 4, 5]

Thanks! It's clear now!

Gali B
Gali B
2,082 Points

Thank you, but why would I'd choose a set and not, let's say a list?

thanks