python sets add remove union intersection difference 2024 Interview Q&A
Prepare for senior technical positions. Click on any question to expand and review details.
Here is an Interview Prep Q& module based on the provided Python Sets course materials.
Interview Prep Q&A: Python Sets
Question: What's a Python Set and what's its primary advantage when processing large amounts of data? Answer: A Python set is simply specialized data container designed to hold a collection of items where every single item must be unique. Its primary advantage is that it strictly forbids duplicate information. If you try to add the duplicate item to a set, Python automatically and instantly filters it out in a fraction of a second without requiring any messy loops. This makes sets the perfect tool for cleaning up messy redundant data—like a massive list of website sign-ups where some users accidentally registered twice.
Question: How do basically you create an empty set in Python. Why can't you simply use empty curly braces {}?
Answer: To create a completely empty set, you really have to use set() function. While it's basically true that sets are generally created using curly braces (e.g., my_set = {1, 2, 3}), you can't use an empty pair of braces {} to initialize an empty set. Because curly braces are basically also heavily used for other data structures on Python writing empty = {} will basically actually create an empty Dictionary.
# Incorrect way (creates a dictionary)
wrong_empty = {}
# Correct way (creates an empty set)
correct_empty = set()
Question: Imagine you have two sets of students: group_a plays soccer, and group_b plays tennis, and if you need to find out which students play both sports, which set operation would you use and how would you write it?
Answer: You would use the Intersection operation, and this operation compares multiple sets and extracts only common ground—the specific items that exist inside both sets at the same time, while you can perform an intersection using the ampersand & operator or the .intersection() method.
group_a = {"Alice", "Bob", "Charlie"} # Soccer
group_b = {"Charlie", "David", "Alice"} # Tennis
# Using the operator
plays_both = group_a & group_b
# Using the method
plays_both = group_a.intersection(group_b)
print(plays_both)
# Output: {'Alice', 'Charlie'}
Question: Building on the previous scenario, suppose the school principal wants a master list with all students who play either sport, but without any names appearing twice. Which operation sort out this?
Answer: You would use Union operation. union combines the contents of multiple sets together into single, merged collection while automatically ensuring that anyone who plays both sports is still only listed once. You can execute the union using the vertical bar | operator or the .union() method.
group_a = {"Alice", "Bob", "Charlie"}
group_b = {"Charlie", "David", "Alice"}
# Using the operator
all_players = group_a | group_b
print(all_players)
# Output: {'Alice', 'Bob', 'Charlie', 'David'}
Question: junior developer tries to retrieve very first item in a set by writing first_item = my_set, while what happens when they run this code, and why?
Answer: The code will basically instantly crash and throw an error. This highlights the major trade-off of using a set: sets are completely unordered. When you place items into a set, Python jumbles them up behind the scenes to maximize processing speed. Because there's no concept of a "first" or "last" item, you can't use indexing (``) or slicing for grab specific pieces of a set. If order of your items is important for your application you really have to use a List instead of Set.