Share how learning Python through DSA problems helps you understand lists, dictionaries, sets, iterators, and built-in functions in context.
Why I started learning Python through Data Structures and Algorithms instead of memorizing syntax first.
When I started learning Python, I realized something: I didn't want to spend hours memorizing syntax without knowing when or why I would use it.
Learning about variables, loops, lists, dictionaries, and functions is important. But reading about these concepts is different from using them to solve an actual problem.
So I decided to change my approach.
Instead of trying to learn all of Python first and then moving on to Data Structures and Algorithms (DSA), I started learning Python while solving problems.
And this changed how I approached programming.
The Problem With Memorizing Syntax
Imagine learning these Python concepts:
- Lists
- Dictionaries
- Sets
- List comprehensions
- Functions
- Iterators
- Built-in methods
You might understand what each concept does. You might even remember its syntax.
But then someone gives you a problem:
Given a list of integers, find the first number that appears only once.
Suddenly, knowing the syntax isn't enough. You need to decide how to solve the problem.
Should you use a dictionary? A nested loop? A set?
This is where programming becomes more than memorizing syntax. You need to understand the problem, identify the right data structure, and translate your reasoning into code.
That's what I wanted to practice.
Learning Python Through a Real Problem
Let's take a simple example.
Problem: Find the first non-repeating element in a list.
nums = [4, 5, 1, 2, 1, 4, 5]
The answer is 2, because it is the first number that appears exactly once when we scan the list from left to right.
Step 1: Think About the Problem
Before writing code, I ask myself two questions:
- How can I count how many times each number appears?
- How can I preserve the original order while finding the first number with a frequency of one?
A Python dictionary is useful for counting frequencies, and the original list preserves the order in which the numbers appear.
Step 2: Write the Solution
def first_non_repeating(nums):
freq = {}
for num in nums:
freq[num] = freq.get(num, 0) + 1
for num in nums:
if freq[num] == 1:
return num
return -1
nums = [4, 5, 1, 2, 1, 4, 5]
print(first_non_repeating(nums))
Output:
2
Step 3: Understand the Python Concepts Naturally
While solving just one problem, I used several Python concepts.
1. Functions
def first_non_repeating(nums):
A function lets me package the solution into a reusable unit that accepts input and returns an answer.
2. Dictionaries
freq = {}
A dictionary stores key-value pairs. Here, each number is a key, and its frequency is the corresponding value.
3. The get() method
freq[num] = freq.get(num, 0) + 1
This line checks whether the number already exists in the dictionary.
- If it exists,
get()returns its current count. - If it doesn't exist,
get()returns0. - We add
1and store the updated count.
For example, when the number 4 appears for the first time, its frequency becomes 1. When it appears again, its frequency becomes 2.
4. Loops
The first loop counts the occurrences. The second loop finds the first number whose frequency equals one.
Notice that we use two loops for different purposes. The first builds the information we need; the second uses that information to find the answer.
5. Conditional statements
if freq[num] == 1:
This checks whether a number occurs exactly once.
I didn't need to memorize every dictionary method before solving this problem. I learned how a dictionary works by using it to solve a concrete problem.
That is the difference I was looking for.
My Learning Approach: Break Problems Into Small Pieces
Whenever I encounter a new problem, I try not to jump straight into code.
I break it down into smaller questions:
- Understand the input and output. What am I given, and what must I return?
- Identify the pattern. Does the problem involve counting, searching, sorting, two pointers, recursion, or dynamic programming?
- Choose a data structure. Would a list, dictionary, set, stack, or queue simplify the solution?
- Build the logic. Can I explain the approach in plain language before implementing it?
- Write the code. Translate each logical step into Python.
- Analyze the solution. Check edge cases and understand the time and space complexity.
This process helps me learn both problem-solving and the language at the same time.
The objective isn't to avoid learning syntax. It's to learn syntax in a context where I understand why it matters.
What About Python Concepts I Don't Know?
Suppose I encounter a problem where I need to remove duplicates while preserving the original order.
I might initially think of using a set.
But then I need to ask: Does a set preserve the order I need for this particular task? How can I track which elements I have already seen?
One possible solution is:
nums = [3, 1, 3, 2, 1, 4]
seen = set()
result = []
for num in nums:
if num not in seen:
seen.add(num)
result.append(num)
print(result)
Output:
[3, 1, 2, 4]
While solving this problem, I learn:
- How to create a set.
- How to check membership using
in. - How to add elements using
.add(). - How to append elements to a list.
- Why using a set to track previously seen elements is useful.
More importantly, I learn that different data structures serve different purposes. The list maintains the desired output order, while the set helps check whether an element has appeared before.
One problem gives me several connected concepts to understand and practice.
Does This Mean I Should Skip Python Fundamentals?
No.
Python fundamentals are essential. Understanding variables, data types, loops, functions, mutability, scope, and exceptions makes it easier to write correct and maintainable code.
The distinction is in the learning sequence.
Instead of treating every concept as a separate chapter that must be completed before I can solve problems, I learn the fundamentals and reinforce them through practical use.
For example:
- Learn dictionaries while solving frequency-counting problems.
- Learn sets while solving membership and duplicate problems.
- Learn recursion while solving tree and backtracking problems.
- Learn sorting while solving ordering and interval problems.
- Learn generators and iterators when a problem or application benefits from processing values lazily.
- Learn classes and object-oriented programming while designing objects with state and behavior.
Some concepts, such as exception handling, modules, file operations, testing, and asynchronous programming, also deserve dedicated practice beyond DSA. Solving algorithmic problems alone will not teach you everything needed to build real Python applications.
How I Practice
My routine is simple:
First, I solve the problem conceptually.
I try to understand the logic without worrying about Python syntax immediately.
Then, I implement the solution in Python.
If I forget a method or don't know the correct syntax, I look it up instead of getting stuck trying to memorize it.
Next, I understand every line.
I don't want code that merely passes test cases. I want to know why it works and what each operation contributes.
Finally, I revisit the problem.
Can I write the solution without looking at the previous implementation? Can I explain its complexity? Can I solve a variation of the same problem?
This final step matters because recognizing a solution is not the same as being able to reproduce and adapt it independently.
The Biggest Lesson
For me, the most important shift was changing the question I ask while learning.
Instead of asking:
“How many Python concepts have I memorized?”
I started asking:
“Can I use the concepts I know to solve a problem, and can I explain why my solution works?”
This doesn't make syntax unimportant. It makes syntax part of a larger learning process.
Programming is not just about knowing which methods exist. It's about breaking a problem into smaller pieces, selecting the right tools, and connecting those pieces into a working solution.
That's the approach I'm following as I learn Python through DSA.
I'm still learning, and there are plenty of concepts I haven't mastered yet. But I'd rather understand a concept through a problem, use it, make mistakes, and revisit it than memorize it without knowing where it fits.
My takeaway: Don't wait until you know all of Python to start solving problems. Learn the fundamentals, start solving problems, and let those problems reveal which concepts you need to understand more deeply.
What about you?
Do you prefer learning a programming language from the fundamentals first, or learning it while solving DSA problems? I'd be interested to hear what has worked for you.
Top comments (0)