Python Sets

Question Click to View Answer

What does the following code print to the console?

n = {1, 10, 4}
print(type(n))

<class 'set'> is printed to the console.

Sets are unordered collections of unique items. Sets cannot contain duplicate items.

Both sets and dictionaries are enclosed by curly braces ({}), so they look similar at a quick glance. Remember that dictionaries are a collection of key / value pairs, whereas sets are unordered collections of unique items.

What does the following code print to the console?

n = {1, 1, 1, 20, 30}
print(n)

{1, 20, 30} is printed.

Sets cannot have duplicates. Python will automatically remove the duplicate items when creating a set.

What does the following code print to the console?

stuff = {"a", 8, "cat"}
stuff.add("dog")
print(stuff)

{'a', 8, 'cat', 'dog'} is printed.

The add() method is used to add a "dog" item to the stuff set.

What does the following code print to the console?

nums = {1, 2, 3}
nums.add(3)
print(nums)

{1, 2, 3} is printed.

Sets only contain unique values, so the add() method cannot add an item to a set if the set already contains that item.

What does the following code print to the console?

things = {"paper", "rock", "bottle"}
things.discard("bottle")
print(things)

{'rock', 'paper'} is printed.

The discard() method deletes an item from a set.

What does the following code print to the console?

games = {"mariocart", "zelda", "splatoon"}
print(games[0])

This code raises a TypeError with the following message: 'set' object does not support indexing.

Sets are unordered, so they can't be indexed.

Dictionaries are also unordered and cannot be indexed. Lists are ordered and can be indexed.

What does the following code print to the console?

sports = {"hockey", "golf"}
print("golf" in sports)

True is printed.

The in operator returns True if the left operand ("golf") is included in the set (sport).

What does the following code print to the console?

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a.union(b))

{1, 2, 3, 4, 5} is printed.

The union() method returns all the elements from set a and set b. The union set doesn't contain any duplicates because sets cannot contain duplicates by definition.

What does the following code print to the console?

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a.intersection(b))

{3, 4} is printed.

The intersection() method returns all the elements that are in both set a and set b. Both sets contain 3 and 4.

What does the following code print to the console?

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a.difference(b))

{1, 2} is printed.

The difference() method returns all the elements that are in set a, but are not in set b.

b.difference(a) would return {5}.

What does the following code print to the console?

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a.symmetric_difference(b))

{1, 2, 5} is returned.

The symmetric_difference() method returns all the items that are in set a and in set b, but not in both set a and set b.

The symmetric_difference() can be thought of as the union minus the intersection.

Notice that b.symmetric_difference(a) would also return {1, 2, 5}.