DEV Community

Samwel Njihia
Samwel Njihia

Posted on

Python 102: Introduction to Data Structures and Algorithms With Python.

This article is beginner friendly to kick start learning data structure and algorithms using python programming language. This article discusses the in-built data structures such list, tuples, set, dictionaries, and user defined data structures such linked list, graph, trees etc., and algorithms. Data is crucial which means that it needs to be stored and accessed in timely manner (This is archived via data structures). The articles discussed data structured and algorithms in python.

Data Structure Definition

Refers to data organization, storage, and data management format that enables efficient modification and access. Data structure allows organization of data in such a way that storage of data collection, relation and and performance of operation between them is possible.

Type of Data Structures in Python

The following is a simple breakdown:
Types of data Structures

a.) Built-in Data Structures

These structures are called List, Dictionary, Tuple and Set.

i.) List

They have positive and negative index, the +ve index starts from 0 and the -ve index starts from -1.

  • Elements are ordered, changeable and allow duplicates
  • Items can be added or removed
  • Defined using [] or the list() constructor
  • Use dot(.) to get in-build method to perform sort, remove etc


my_list = [] #empty list
my_list = [10, 22, 3, 'Sam', 3.132] #list with data
Enter fullscreen mode Exit fullscreen mode


[10, 22, 3, 'Sam', 3.132]
Enter fullscreen mode Exit fullscreen mode

Some of operations that can be performed using List are append

ii.) Dictionary

They are used to store key value pairs.

  • They are ordered , changeable, and do not allow duplicates(from python 3.7 and above)
my_dict = {} #empty dictionary
my_dict = {1: 'Django', 2: 'Laravel'} #With elements
Enter fullscreen mode Exit fullscreen mode


{1: 'Django', 2: 'Laravel'}
Enter fullscreen mode Exit fullscreen mode

The following are operations that can be performed using Dictoionary

iii.) Tuple

They are the same as lists with the exception that the data once entered into the tuple cannot be changed no matter what.

  • Items are ordered, unchangeable, and allow duplicates values
  • Items can be accessed by referring to the index number, inside square brackets
names= ("apple", "banana", "cherry")
Enter fullscreen mode Exit fullscreen mode


('apple', 'banana', 'cherry')
Enter fullscreen mode Exit fullscreen mode
iv.) Sets

They are a collection of unordered elements that are unique. Meaning that even if the data is repeated more than one time, it would be entered into the set only once.

  • Unordered, unchangeable, unindexed, and do not allow duplicate values
  • Items in a set cannot be accessed using index but can be accessed using loop #####Example:
num_set = {1, 3, 6, 70}
print(num_set )
Enter fullscreen mode Exit fullscreen mode


{1, 3, 6, 70}
Enter fullscreen mode Exit fullscreen mode

Operation in set are Union, intersection, Difference etc


my_set = {1, 2, 3, 4}
my_set_2 = {3, 4, 5, 6}
print(my_set.union(my_set_2), '----------', my_set | my_set_2)
print(my_set.intersection(my_set_2), '----------', my_set & my_set_2)
print(my_set.difference(my_set_2), '----------', my_set - my_set_2)
print(my_set.symmetric_difference(my_set_2), '----------', my_set ^ my_set_2)
Enter fullscreen mode Exit fullscreen mode


{1, 2, 3, 4, 5, 6} ———- {1, 2, 3, 4, 5, 6}
{3, 4} ———- {3, 4}
{1, 2} ———- {1, 2}
{1, 2, 5, 6} ———- {1, 2, 5, 6}
Enter fullscreen mode Exit fullscreen mode
b.) User defined Data Structures

These include Queue, tree, Linked List, Stack, graph, map etc

NB Array Vs List

They are the same with one difference. Lists allow heterogeneous data element storage whereas Arrays allow only homogeneous elements to be stored within them.

i.) Stacks

They are linear Data Structures which are based on the principle of Last-In-First-Out (LIFO) where data which is entered last will be the first to get accessed. Built using the array structure and has operations namely, pushing (adding) elements, popping (deleting) elements and accessing elements only from one point in the stack called the TOP.TOP is the pointer to the current position of the stack

Operation in Stacks

Adds an element at the top of the stack


Removes an element from the top of the stack

ii.) Queue

Is a linear data structure which is based on the principle of First-In-First-Out (FIFO) where the data entered first will be accessed first.It is built using the array structure and has operations which can be performed from both ends of the Queue, i.e, head-tail or front-back. Operations such as adding and deleting elements are called En-Queue and De-Queue and accessing the elements can be performed.

iii.) Tree

They are non-linear data structure that have roots and node.The root is the node from where the data originates and the nodes are the other data points that are available to us. The node that precedes is the parent and the node after is called the child. There are levels a tree has to show the depth of information. The last nodes are called the leaves.

iv.) Linked List

Linked lists are linear Data Structures which are not stored consequently but are linked with each other using pointers. The node of a linked list is composed of data and a pointer called next.
Linked lists

v) Graph

Graphs are used to store data collection of points called vertices (nodes) and edges (edges). Graphs can be called as the most accurate representation of a real-world map. They are used to find the various cost-to-distance between the various data points called as the nodes and hence find the least path.

v) HashMaps

are the same as what dictionaries are in Python. They can be used to implement applications such as phonebooks, populate data according to the lists


They are instructions or rules that are formulated in a finite sequential order to solver problems and get results. They provide pseudo-code for problems and they are not language specific as they can be implemented in any language.

How to write an algorithm
  1. Figure out what is the exact problem
  2. Determine where you need to start
  3. Determine where you need to stop
  4. Formulate the intermediate steps
  5. Review your steps #####Algorithm Classes: Algorithms classes Some examples of algorithms include Tree Traversal Algorithms, Searching Algorithms, Sorting Algorithms, etc.

Top comments (2)

brayan_kai profile image
Brayan Kai

Great article keep it up @samgnjihia lets keep moving 🌟

samgnjihia profile image
Samwel Njihia

Thanks fellow geek