Data Structure Comparison
Choose a set when membership and uniqueness matter more than order or frequency. Choose a list when order or duplicate values must be preserved.
Try it: Sets: Unique Members and Venn Operations
How a Python set keeps each value once, answers membership, and combines two sets by membership alone: union, intersection, difference and symmetric difference.
How it works
- set(list) adds each value; a value already in the set changes nothing.
- x in s asks only whether the value is present (no position, no count).
- Every member is only in A, only in B, or in both.
- A | B keeps all, A & B the shared ones, A - B those only in A, A ^ B those in exactly one.
- A set has no order of its own, so the lab shows members sorted, as sorted(s) would.
Default run (16 steps): Build two sets from lists. Sets keep no order, so members are drawn sorted here (like sorted(s)); Python's own print order may differ. … sorted(A & B) → ['india', 'peru'] (2 members; sorted only for display).
Simplified: Up to 8 short words or ints per set; the members are displayed sorted because Python's set iteration order depends on hashing and is not meaningful.
Loading the simulation…
Why Membership Matters
A collection can be useful for different reasons. Sometimes the order of items matters, or you need to keep repeated values. In other situations, the important question is simply whether an item belongs to the collection. A set is designed for that second situation: it is an unordered collection of unique elements.
Choose a set when membership and uniqueness matter more than order or frequency. Choose a list when order or duplicate values must be preserved.
Creating a Unique Collection
In Python, a set can be created with set() or with curly braces. Passing a list or another iterable to set() converts its elements into an unordered collection and automatically removes duplicates. The resulting set contains each distinct element only once.
Building the bri set
Create a set named bri containing the country names brazil, russia, and india.
- 1Create the collection
Pass the country values to
set(). The result is an unordered collection of unique country names. - 2Check uniqueness
If one of the country names had appeared more than once in the input, the set would keep only one copy.
- 3Interpret the result
The set contains three distinct country names. Their written order does not define an important position inside the set.
bri contains brazil, russia, and india as unique elements.
Membership and Mutation
The in operator tests whether an element belongs to a set. The add() method inserts an element, while remove() deletes an element. Adding an element that is already present does not change the set, does not increase its size, and does not raise an error.
Checking and changing country sets
Use the source sequence involving bri and bric to trace membership tests and set mutations.
- 1Test membership
The membership test for india in bri is true because india belongs to bri. The test for usa in bri is false because usa does not belong to bri.
- 2Make an independent copy
Create bric as a copy of bri so that it can be modified independently.
- 3Add an element
Add china to bric. bric now contains all elements of bri plus china, so bric is a superset of bri.
- 4Remove an element
Remove russia from bri. bri is left with brazil and india.
bri contains brazil and india, while bric contains brazil, russia, india, and china.
Relationships Between Sets
Set operations compare collections by their members. An intersection contains only the elements present in both sets. A union combines the elements from both sets without repeating shared elements. A difference identifies elements in one set that are not in the other. These operations help describe overlap and other relationships between collections.
Finding the overlap
Find the intersection of bri and bric after the membership and mutation sequence.
- 1List the members of bri
After russia is removed, bri contains brazil and india.
- 2List the members of bric
After china is added to its copied collection, bric contains brazil, russia, india, and china.
- 3Keep shared elements
The intersection keeps only elements that appear in both collections.
The intersection contains brazil and india.
The intersection is the overlap between two sets. In the source example, both the ampersand operator and the intersection() method produce the same shared elements: brazil and india.
Set or List
| Question | Set | List |
|---|---|---|
| Are duplicate values kept? | No. Duplicate values are automatically eliminated. | Yes. Repeated values remain. |
| Does order matter? | No. Sets are unordered. | Yes. Lists preserve order. |
| Is indexing supported? | No. Sets do not support indexing. | Lists support ordered positions. |
| What is the main use? | Testing membership and comparing collections. | Preserving order or storing duplicates. |
The choice depends on whether membership, uniqueness, order, or frequency is most important.
Before choosing a collection, identify the information your program must preserve. Use a set if repeated entries should collapse into one member and the main question is whether an item belongs. Use a list if the sequence itself matters or if repeated entries carry meaning.
Frequent Selection Errors
Common mistake Expecting a set to preserve the order in which values were written.
What it looks likeTreating the positions of brazil, india, and russia as meaningful after placing them in a set.
Why it failsSets are unordered collections, so order is not the property they are designed to preserve.
Do this insteadUse a list when the order of values matters.
Common mistake Expecting a set to retain duplicate values.
What it looks likeAdding brazil to a set that already contains brazil and expecting two copies.
Why it failsSets automatically enforce uniqueness. Adding an existing element leaves the set unchanged.
Do this insteadUse a list when duplicate occurrences need to remain available.
Common mistake Trying to access a set by an index.
What it looks likeTreating a set as though it had a first, second, or third position.
Why it failsSets do not support indexing.
Do this insteadUse membership tests or set operations, or choose a list when indexed positions are required.
Common mistake Using a set when frequency is important.
What it looks likeConverting repeated values into a set when the number of occurrences must be preserved.
Why it failsDuplicate values are removed, so the set cannot represent repeated occurrences in the same way as a list.
Do this insteadUse a list when frequency or duplicate storage matters.
Apply the Choice
A collection must answer whether a country is present, ignore repeated country names, and compare its members with another collection. Decide whether a set or a list is the better choice, then name the operation you would use to find the countries shared by both collections.
Hints
- Membership and uniqueness are more important here than order.
- The operation for values present in both sets is the intersection.
What do you think happens?
Suppose a set already contains brazil and you add brazil again. What changes?
Reveal answer
Nothing changes.
Sets enforce uniqueness. Adding an element that is already present leaves the set the same size and raises no error.
Key Takeaways
- 1A set is an unordered collection of unique elements.
- 2Use
set()or curly braces to create a set, and remember that duplicate values are removed automatically. - 3Use in for membership tests,
add()to insert an element, andremove()to delete an element. - 4Use intersection, union, and difference to find relationships between collections.
- 5Choose a set for membership and uniqueness; choose a list for order or duplicate values.
At a glance
- Sets store unique elements without treating their order as significant.
- Membership tests and basic mutations let you check, add, and remove elements.
- Set operations reveal shared, combined, and differing elements between collections.
- Lists are preferable when order or duplicate values must be preserved.