Sitemap

Data Structures and Algorithms in Python —

5 min readOct 18, 2026

--

Imagine you are the manager of a big library called Python Library. Thousands of books are stored inside it, and visitors want to find books quickly. How will you organize the books? How will you find a particular book without checking every shelf?

This is where Data Structures and Algorithms (DSA) help us!

1. What are Data Structures?

A data structure is a way to organize and store data so that we can use it efficiently.

Think of data structures as different types of storage boxes in your library.

Press enter or click to view image in full size

1. List — A row of books

A Python list stores multiple items in order. You can access an item using its index.

books = ["Python", "Java", "HTML"]print(books[0])  # Python
Press enter or click to view image in full size

2. Dictionary — A book catalogue

A dictionary stores information as key-value pairs.

student = {    "name": "Aman",    "age": 18}print(student["name"])  # Aman
Press enter or click to view image in full size

3. Stack — Books piled on top of each other

A stack follows LIFO: Last In, First Out. The last book placed on top is the first one removed.

stack = []stack.append("Book A")stack.append("Book B")print(stack.pop())  # Book B
Press enter or click to view image in full size

4. Queue — A line of people

A queue follows FIFO: First In, First Out. The first person to join is the first person served.

from collections import dequequeue = deque(["Aman", "Riya"])print(queue.popleft())  # Aman

Other important data structures include sets, linked lists, trees, graphs, and heaps.

2. What are Algorithms?

An algorithm is a step-by-step set of instructions to solve a problem.

Imagine a student named Riya wants to find the book Python in a library. She needs a strategy to find it.

Here are two ways she could do it.

Press enter or click to view image in full size

Algorithm 1: Linear Search

Riya checks each book one by one until she finds Python.

books = ["HTML", "CSS", "Python", "Java"]for book in books:    if book == "Python":        print("Book found!")        break

This works even if the list is not sorted.

Press enter or click to view image in full size

Algorithm 2: Binary Search

If the books are sorted, Riya can repeatedly check the middle and eliminate half of the remaining choices.

Binary search is much faster for large sorted lists.

books = ["CSS", "HTML", "Java", "Python"]left = 0right = len(books) - 1while left <= right:    mid = (left + right) // 2    if books[mid] == "Python":        print("Book found!")        break    elif books[mid] < "Python":        left = mid + 1    else:        right = mid - 1

3. Sorting algorithms — Organizing the library

Riya discovers that the books are mixed up. She decides to arrange them in alphabetical order.

Sorting means arranging data in a particular order, such as ascending or descending.

For example:

[5, 2, 8, 1, 3]

After sorting:

[1, 2, 3, 5, 8]

Python makes sorting easy:

numbers = [5, 2, 8, 1, 3]numbers.sort()print(numbers)# [1, 2, 3, 5, 8]

Some sorting algorithms you should learn are:

  • Bubble Sort: repeatedly swaps neighboring items that are in the wrong order.
  • Selection Sort: repeatedly selects the smallest remaining item.
  • Insertion Sort: inserts each item into its correct position.
  • Merge Sort: divides the data, sorts the parts, and combines them.
  • Quick Sort: partitions the data around a chosen pivot.

4. Time complexity — How fast is your solution?

One day, the library receives one million books. Riya realizes that finding a book one by one could take a long time.

Get Raveenanagar’s stories in your inbox

Join Medium for free to get updates from this writer.

Programmers use Big O notation to describe how an algorithm’s work grows as the input gets larger.

Big O

Meaning

Example

\(O(1)\)

Constant time

Accessing a list item by index

\(O(\log n)\)

Grows slowly

Binary search

\(O(n)\)

Grows with input size

Linear search

\(O(n \log n)\)

Efficient for many large sorting tasks

Merge sort

\(O(n²)\)

Can grow quickly

Bubble sort in the general worst case

The smaller growth rate is generally preferable for large inputs, although the best choice depends on the problem.

5. More DSA concepts in Python

Press enter or click to view image in full size

Trees

Like a family tree, data is organized into parent and child nodes. Trees are used in file systems and search structures.

Graphs

Like a map connecting cities, graphs represent objects and the relationships between them. They are useful for navigation and social networks.

Heaps

A heap helps retrieve the highest- or lowest-priority item efficiently. Python provides heapq for working with heaps.

6. Your DSA learning journey

Follow this order to build your skills:

1. Python basics and functions

2. Lists, tuples, dictionaries and sets

3. Searching and sorting algorithms

4. Time and space complexity (Big O)

5. Stacks, queues and linked lists

6. Recursion

7. Trees, heaps and graphs

8. Practice coding problems

7. Practice with a small challenge

Question: Riya has this Python list:

numbers = [10, 20, 30, 40, 50]

Which expression accesses the number 30?

numbers[1]

numbers[2]

numbers[3]

Remember: Data structures organize your data, and algorithms tell your program how to solve problems using that data. Together, they help you write efficient Python programs and prepare for coding interviews and developer roles.

Press enter or click to view image in full size

--

--