DEV Community

Jason Omondi
Jason Omondi

Posted on

Introduction to Data Structures and Algorithms With Python.

Python is a programming language that is taking over the market. It is very essential for you to learn its ways of storing and manipulating data for a smooth programming experience.
You should be able to appreciate by the end of this article that data structures are very powerful and can be used to solve very complex problems in the world of computing.
Let’s hop right into it. So Data structures are just ways or means of storing data such that operations can be done on the data which is stored in computers more efficiently.
On the other hand Algorithms are step by step instructions of how to solve a particular problem.

We are going to discuss the following structures:
• Lists
• Dictionaries
• Tuples
• Sets
• Queues
• Stack
• Linked lists

Lists
These are just ordered collections of data that are stored together in the same variable. It sounds similar to arrays.
The basic syntax of declaring a list is by enclosing the list elements in square brackets
Example: name = [1,2,3,4,5…]
The elements in the list can be accessed through indexes. The index counts from 0 which is the first element in the list all the way to N-1 where N is the total number of elements in the list.
There are operations that are made to the list and we are going to briefly state them as follows:
- Append – adds elements to the end of the list
- Remove – removes elements from the list in case of two
occurrences of the same element
- Pop – removes elements at any position on the list
- Insert – adds elements at any position on the list
- Len – returns the length of the list i.e. number of items
These are just but a few but in case you want to advance to more complex operations you can refer to articles on the web. We are trying to be as basic as possible so as not to intimidate newbies.

Dictionaries
These are collections of data values that uses a key and a value to store the data. It is unlike other data types that store same collections of data in the sense that it uses the key: value pair in it storage.
The keys are case sensitive in the sense that ‘Myname’ as a key is different from ‘myName’.
The keys can never be duplicated, it can only appear once in the dictionary.
Values can be repeated and of any data type, it causes no harm.
This is the syntax for a dictionary in python: name = {1: ‘Nairobi’, 2: ‘Kampala’, 3: ‘Arusha’…}

You can straight away realize the difference between dictionary and a list is the square brackets and the curly brackets.
Operations on the dictionary in python
These are a handful of operations that can be used:
1. Update items
2. Copy items
3. Get values from the dictionary
4. Clear the dictionary
5. Get the keys that are on the dictionary
There are other operations so don’t be confined to these few which are just meant to enlighten you about basic information about data structures.

Tuples
It is also a collection but now of pythons objects that are comma separated and they are stored in an ordered sequence. It is also immutable in the sense that it doesn’t change.
The syntax of declaring a tuple is : variableName = (‘Nairobi’, ‘Kisumu’, ‘Mombasa’… )
Tuples can be joined together and the data in them printed together. This is called Concatenation.
You just use the ‘+’ sign to join to tuples together i.e. tuple1 + tuple2.
Operations on the tuple in python
The items in the tuple can be:
- Deleted
- Updated
- You can get length of the tuple
- Perform repetition of items
- Loop through the items

Sets
Like lists, sets are used to store multiple items or a collection of data in a single variable.
Items in a set are usually unordered, unindexed, and immutable.
The basic syntax is the same as that of the dictionary. We use curly brackets to define the values or items in the set.
Duplicates are not allowed in a set. Also it can accept a wide range of data types.
Syntax: variableName= {‘Nairobi’, ‘Naivasha’, ‘Nakuru’…}
Operations on the set in python
- Union – this is used to get a new set of elements from two
sets which are being joined.
- Intersection – returns elements that are common to two
sets
- Difference – returns items that are in the first set and
not in the second set
- Symmetric difference – returns a set containing elements
in both sets but exclude the common ones.
- You can also copy, clear, add and remove in a set.

Queues
These are linear data structures that store there item in FIFO (First In First Out). This means that the least recent item to be added to the queue will be the first to be removed.
Because of the linear feature, they don’t allow random access of items from any position.
You can implement a queue in python using lists, collections.deque or queue.Queue.
Operations on the queue in python
These are the operations that can be made on a queue:
- Enqueue - this operation adds items to the queue.
- Dequeue – it removes items from the queue where by the
items are popped out in the same order which they were
pushed in.
- Front – it gets the item at the front of the queue.
- Rear – gets the last item from the queue.

Stacks
It is also a linear data structure but now this can store its items either in LIFO (Last In First Out) or FILO (First In Last Out).
The last item or the first item to be added can be first to be accessed for manipulation. If an element is added from a specific end, it will only be accessed through that end.
The implementation of stacks is basically the same as queues.
Operations on the stack in python
- Push – it inserts elements to a stack
- Pop – it delete elements from a stack
- Top – returns a reference to the topmost element in the
stack.
- Empty – it checks if the stack is empty or not.

Linked lists
These are collections or sequence of data elements that are connected together via links.
The links are inform of pointers, which link every element to another element.
With linked list it is easy to perform insertion and deletion operations because of the pointers.
However due to the pointers being in the picture, they have to be stored therefore you end up with double storage on a single element. You can also not access the elements randomly cause of the linear feature.
They can also store any data type.
In python linked lists are created using the node class which can be manipulated effectively.
Types of linked lists
1. Singly linked list – contains only a link to the next
element
2. Doubly linked list – contains a link to the next and
previous elements
3. Circular linked list – all the nodes are connected to
forma circle.
Operations on the linked list in python
- Insert items to the list
- Delete items
- You can print the list out
- Get the size of the list
- You can search a given element from the list
- Traverse the nodes one after the other
- Sort the list

Top comments (0)