DEV Community

Samiksha Srivastav
Samiksha Srivastav

Posted on

Python Sets & Tuples: What I Actually Learned While Preparing for DSA

While preparing for DSA and technical interviews, I realized something pretty quickly:

Knowing Python syntax is not the same as knowing when to use it.

So instead of trying to finish Python as quickly as possible, I've been following a different approach:

Learn a concept → understand the practical use → create a cheat sheet → solve problems → identify patterns.

Recently, I completed two Python topics that look simple on the surface but become surprisingly useful when solving problems:

  • Sets
  • Tuples

This is what I learned.


1. Sets

A set is an unordered collection of unique elements.

numbers = {1, 2, 3, 4}
Enter fullscreen mode Exit fullscreen mode

Unlike lists, sets don't keep duplicate values.

numbers = [1, 2, 2, 3, 4, 4]

unique_numbers = set(numbers)

print(unique_numbers)
Enter fullscreen mode Exit fullscreen mode

The duplicates are automatically removed.

The most important pattern: seen

One of the most useful things I learned from sets was the seen pattern.

Suppose I want to check whether a list contains a duplicate.

Instead of maintaining a separate list of duplicates, I can ask:

"Have I seen this value before?"

numbers = [1, 2, 3, 4, 5, 3, 2]

seen = set()

for num in numbers:
    if num in seen:
        print(True)
        break

    seen.add(num)
Enter fullscreen mode Exit fullscreen mode

The moment a value appears again, we know a duplicate exists.

This pattern is much more important than simply knowing how to create a set.


2. The Four Set Operations I Need to Remember

Suppose:

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
Enter fullscreen mode Exit fullscreen mode

Union — |

Everything from both sets:

A | B
Enter fullscreen mode Exit fullscreen mode

Result:

{1, 2, 3, 4, 5, 6}
Enter fullscreen mode Exit fullscreen mode

Intersection — &

Elements common to both:

A & B
Enter fullscreen mode Exit fullscreen mode

Result:

{3, 4}
Enter fullscreen mode Exit fullscreen mode

Difference — -

Elements in A but not B:

A - B
Enter fullscreen mode Exit fullscreen mode

Result:

{1, 2}
Enter fullscreen mode Exit fullscreen mode

Symmetric Difference — ^

Elements present in exactly one of the sets:

A ^ B
Enter fullscreen mode Exit fullscreen mode

Result:

{1, 2, 5, 6}
Enter fullscreen mode Exit fullscreen mode

My mental shortcut is:

|  → EVERYTHING
&  → COMMON
-  → ONLY LEFT
^  → EXACTLY ONE
Enter fullscreen mode Exit fullscreen mode

This became especially useful when solving problems written in natural language.

For example:

"Students registered but not logged in"

Immediately becomes:

registered - logged_in
Enter fullscreen mode Exit fullscreen mode

And:

"Students attending both sessions"

becomes:

morning & evening
Enter fullscreen mode Exit fullscreen mode

The goal is to recognize the operation from the wording.


3. Sets and the Two Sum Pattern

Another important pattern was using a set to remember values we've already encountered.

For:

numbers = [2, 7, 3, 9]
target = 10
Enter fullscreen mode Exit fullscreen mode

Instead of checking every possible pair, we can calculate what value we need:

needed = target - num
Enter fullscreen mode Exit fullscreen mode

Then check whether that value has already been seen.

seen = set()

for num in numbers:
    needed = target - num

    if needed in seen:
        print(True)
        break

    seen.add(num)
Enter fullscreen mode Exit fullscreen mode

The important idea isn't just the code.

It's:

current value → calculate required complement → check whether complement was seen

This is a pattern I expect to see repeatedly in DSA.


4. Tuples

A tuple is an:

ordered, immutable collection of elements that can contain duplicates.

data = (10, 20, 30)
Enter fullscreen mode Exit fullscreen mode

The key difference from a list is mutability.

List   → ordered + mutable + duplicates
Tuple  → ordered + immutable + duplicates
Set    → unique + unordered + mutable
Enter fullscreen mode Exit fullscreen mode

5. Tuple Unpacking

One of the most useful tuple concepts I learned was unpacking.

Instead of:

name = student[0]
age = student[1]
course = student[2]
Enter fullscreen mode Exit fullscreen mode

we can write:

name, age, course = student
Enter fullscreen mode Exit fullscreen mode

Python assigns the values position by position.

This also appears naturally when working with dictionaries:

for key, value in data.items():
    print(key, value)
Enter fullscreen mode Exit fullscreen mode

6. Swapping Values

Python also makes swapping very clean:

a, b = b, a
Enter fullscreen mode Exit fullscreen mode

No manually created temporary variable is required.

The right side is evaluated first and then unpacked into the variables.


7. Returning Multiple Values

A Python function can return multiple values:

def calculate(a, b):
    return a + b, a * b
Enter fullscreen mode Exit fullscreen mode

We can then unpack them:

total, product = calculate(3, 4)
Enter fullscreen mode Exit fullscreen mode

The returned values behave like a tuple:

(7, 12)
Enter fullscreen mode Exit fullscreen mode

and are unpacked into:

total   → 7
product → 12
Enter fullscreen mode Exit fullscreen mode

This is one reason understanding tuples is useful even when you're not explicitly writing tuple-heavy code.


8. Tuple Immutability

This doesn't work:

data = (10, 20, 30)

data[1] = 25
Enter fullscreen mode Exit fullscreen mode

because tuples cannot be modified.

If I need a changed version, I have to create a new tuple.

For example, if every 20 needs to become 25:

data = (10, 20, 30, 20)

new_data = []

for num in data:
    if num == 20:
        new_data.append(25)
    else:
        new_data.append(num)

new_tuple = tuple(new_data)
Enter fullscreen mode Exit fullscreen mode

Result:

(10, 25, 30, 25)
Enter fullscreen mode Exit fullscreen mode

The original tuple remains unchanged.


9. Tuple Slicing

Tuples support slicing just like lists.

Reverse:

data[::-1]
Enter fullscreen mode Exit fullscreen mode

First three elements:

data[:3]
Enter fullscreen mode Exit fullscreen mode

So immutability does not mean we cannot create new data from a tuple.

It means we cannot modify the existing tuple.


10. Extended Unpacking

This was one of my favorite small Python features:

data = (10, 20, 30, 40, 50, 60)

first, *middle, last = data
Enter fullscreen mode Exit fullscreen mode

Now:

first  → 10
middle → [20, 30, 40, 50]
last   → 60
Enter fullscreen mode Exit fullscreen mode

An important detail:

The values captured by *middle become a list.

So even though the original object is a tuple:

type(data)
Enter fullscreen mode Exit fullscreen mode

is:

tuple
Enter fullscreen mode Exit fullscreen mode

while:

type(middle)
Enter fullscreen mode Exit fullscreen mode

is:

list
Enter fullscreen mode Exit fullscreen mode

The Bigger Lesson

The most useful part of learning Sets and Tuples wasn't memorizing methods.

It was learning to translate a problem statement into a pattern.

For example:

"Have I seen this before?"
        ↓
      seen set
Enter fullscreen mode Exit fullscreen mode
"Find common elements"
        ↓
       A & B
Enter fullscreen mode Exit fullscreen mode
"Present in A but not B"
        ↓
       A - B
Enter fullscreen mode Exit fullscreen mode
"Exactly one"
        ↓
       A ^ B
Enter fullscreen mode Exit fullscreen mode
"Take first, middle and last"
        ↓
    *middle unpacking
Enter fullscreen mode Exit fullscreen mode

That's the kind of thinking I want to build before moving deeper into DSA.

I'm currently following a structured Python → Problem Solving → DSA roadmap, and I'm deliberately solving problems topic-by-topic rather than jumping randomly between questions.

The next step is List Comprehensions, followed by the remaining Python fundamentals before moving deeper into problem-solving patterns and DSA.

The goal isn't simply to "finish Python."

The goal is to reach a point where I can look at a problem and start recognizing the underlying pattern.

Top comments (0)