Quick Tip Using Sets in Python

In Python, sets are lists that don’t contain any duplicate entries. Using the set type is a quick and easy way to make sure that a list doesn’t contain any duplicates. Here’s an example of how you would use it to check a list for duplicates:

a = set(["Pizza", "Ice Cream", "Donuts", "Pizza"])
print a

Because “Pizza” is in the list twice, using set will yield an output of the list where “Pizza” only appears once, like this:

set(['Pizza', 'Ice Cream', 'Donuts'])

As you can see, using the set type is a great way to make sure every value in your list is one of a kind.

Leave a Reply

Your email address will not be published. Required fields are marked *