DEV Community

Sarah Mukuti
Sarah Mukuti

Posted on

Introduction to data structures and algorithms with python

This article will analyze data structures and how they are implemented in python.
Towards the end I will also analyze a few sample algorithms.


A list is a mutable and ordered data structure that is defined using the [] brackets. A list can contain items of different data types.

languages = ['CSS', 'Java', 'JavaScript', 'Python', 'Ruby', 'C++',
             'PHP', 'SQL', 'Pascal', 'Swift']
#add a new element to the end of the list
#access element on index 0

Enter fullscreen mode Exit fullscreen mode
 Output : ['CSS', 'Java', 'JavaScript', 'Python', 'Ruby', 'C++', 'PHP', 'SQL', 'Pascal', 'Swift', 'Kotlin']
Enter fullscreen mode Exit fullscreen mode


A tuple is an immutable data structure that contains a sequence of elements. The elements cannot be altered through adding or removing elements.

#Creating a tuple to store the connection values on a server
#The information cannot be changed in the entire program
host, IP, port = ('Local host', '', 8080)
print('The connection variables are {} {} {} '.format(host, IP, port))
Enter fullscreen mode Exit fullscreen mode
Output : The connection variables are Local host 8080
Enter fullscreen mode Exit fullscreen mode


A set is a collection of unique elements. It is unique and unordered. One can remove elements or add unique elements to the set.

#create a set from list of numbers
numbers = [1, 4, 6, 8, 3, 2, 7] 
num_set = set(numbers)
#create a list for programming languages
language = {'CSS', 'Java', 'JavaScript', 'Python', 'Ruby', 'PHP'}
print('CSS' in language)
Enter fullscreen mode Exit fullscreen mode
> Output : {1, 2, 3, 4, 6, 7, 8}
Enter fullscreen mode Exit fullscreen mode


A dictionary stores items in key value pairs. It is mutable and the elements are unordered.

City = {'Kenya':'Nairobi', 'Uganda':'Kampala', 'Ghana':'Accra', 'South Africa':'Pretoria'}
Enter fullscreen mode Exit fullscreen mode
> Output {'Kenya': 'Nairobi', 'Uganda': 'Kampala', 'Ghana': 'Accra', 'South Africa': 'Pretoria'}
Enter fullscreen mode Exit fullscreen mode


A queue is a data structure that stores elements in a sequential manner. It supports two operations to either dequeue or enqueue elements. The queue has the front end that points to the starting element and the rear end that points to the last element. Elements on a queue are removed based on the FIFO(first in first out) principle.


The stack is the opposite of a queue in that elements are accessed following the LIFO(Last in first out) principle. The last element is removed first. The basic operations on stack are; push, pop, IsEmpty, IsFull.

Linked List

A linked list is a data structure where all elements are connected to one another. The connection is made through pointers. To create a linked list we use the node class and pass in the necessary objects.
Image description
A linked list supports some operations such as traversal, insertion, and deletion.


An algorithms is a step-by-step procedure that outlines the instructions that are to be followed when solving a problem. Algorithms are not language specific and can be implemented in any programming language. An algorithms can simply be explained through the many procedures that people follow when doing everyday activities for example when preparing a favorite meal one will always start with getting the ingredients, prepare them, and finally get into the cooking process. Similarly when solving programming problems a programmer will always start by defining the problem, getting necessary inputs, and then following the programming procedure to attain the desired output. Some of the algorithms that can be implemented in python include quick sort, greedy search, and binary search algorithms.

Top comments (1)

brayan_kai profile image
Brayan Kai

Really great article Keep up 😊👏✨