Introduction to Data Structures and Algorithms With Python
Tags: programming, python, algorithms
Published: 2022-02-21 15:15:57

This article deals with introducing data structures and algorithms with Python. Data structure is simply a place or location where data are stored and organized. It is a way of arranging data on a computer to facilitate the access and updating of those data. There are two types of data structure: Linear data structure and non-linear data structure. Algorithm is a collection of ways, steps or stages to go through to solve a particular problem. Algorithms are instructions designed and well established or set of inputs created to produce a desired output.

Let us have an example of data structures with Python
Generally, for storing and access to data, these structures called list, dictionary, tuple and set are very helpful.  For full control over their functionality in python, the most important data structures are Stack, Queue, Tree, Linked List and others. These structures can be found in other programming languages.

It is important to recognize that there are two kinds of data structures designed to implement in Python: 
Built-in Data structures and User defined data structures.

Built-in data structures include: List, Tuple, dictionary and set
User defined data structures: Stack, Queue, Tree, linked list, graph and hashMap.

**Built-in data structures**
These structures help make programming easier because they were constructed in Python. They help find a solution quickly.

**1. Lists**
There are designed to store data of different kinds in a sequential way. They have indexes, that can positive or negative, to facilitate the access.
**Example:**
To create a list, we use a square brackets and put any elements accordingly. If not, you get an empty list as the output
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpjrrcl4qrrku0kpr1jk.png)

If you want to add an element, you use using the _append(), extend() and insert()_ functions.
**Example:**

- _The append()_ function adds all the elements passed to it as a single element.
- The _extend()_ function adds the elements one-by-one into the list.
- The _insert()_ function adds the element passed to the index value and increase the size of the list too.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6r2cgfulotcx6g2gcb3.png)

If you want to delete elements:

- To delete elements, use the del keyword which is built-in into Python but this does not return anything back to us.
- If you want the element back, you use the _pop()_ function which takes the index value.
- To remove an element by its value, you use the _remove() _function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8klrucknxt5djij7jden.png)

If you want to ACCESS the elements: It is similar to strings in python. You use the index values and hence can obtain the values as needed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i80939l9k7pgpdmsktom.png)
There are other functions:

- The _len()_ function returns to us the length of the list.
- The _index()_ function finds the index value of value passed where it has been encountered the first time.
- The _count()_ function finds the count of the value passed to it.
- The _sorted()_ and _sort()_ functions do the same thing, that is to sort the values of the list. The _sort()_ has a return type whereas the _sort()_ modifies the original list.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvsr41gb81aucog30ym6.png)

**2. Dictionaries **
The dictionaries store key-value pairs. To create a dictionary, you use the flower braces or use the _dict()_ function. You will have to add the key-value pairs whenever you work with dictionaries.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfrkmhbo4emadrwmvi66.png)

To change the values of the dictionary, you use the keys. You access the key and then change the value accordingly. To add values, you add another key-value pair as indicated below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzkj2l0ygjng0i9dh697.png)
**How to Delete key-value pairs** 

- To delete the values, you use the _pop()_ function which returns the value that has been deleted.
- To retrieve the key-value pair, you use the _popitem()_ function which returns a tuple of the key and value.
- To clear the entire dictionary, you use the _clear()_ function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmcmx3pdjgxnghdppaj3.png)
**How to access elements **
You use keys only. You can use either the _get()_ function or just pass the key values and you will be retrieving the values.
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxxhwuxvinc9nlqr10zj.png)

**There are other Functions**
These are the: _keys(), values(), items() _functions
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8g7qzdozzrtc073kohk.png)
**3.Tuples** 
Tuples are similar to lists are with the exception that the data once entered into the tuple cannot be changed no matter what. 
To create the tuple, you  use the _tuple()_ function

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icveqy5cqg3lez4yznc1.png)
To access the elements is similar to accessing values in lists.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3eoinutealuss9ddqgpc.png)
**To append the values**: To append the values, use the ‘+’ operator which will take another tuple to be appended to it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/em8gwmesvokvpp06i3ez.png)
There are other functions that are similar to those of the lists.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbvbri9n5n8vxu9vzqfm.png)
**4.Sets**  
Sets are a collection of unordered elements that are unique. To create the sets, using the flower braces but instead of adding key-value pairs, you just pass values to it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxi0u2xcxl9i6wmurgce.png)
With sets, you can operations, such as union, intersection and so on. You do use these following functions:

- The _union()_ function combines the data present in both sets.
- The _intersection()_ function finds the data present in both sets only.
- The _difference()_ function deletes the data present in both and outputs data present only in the set passed.
- The _symmetric_difference()_ does the same as the _difference()_ function but outputs the data which is remaining in both sets.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iw1jdg46yx5wczcbdbuo.png)
**User-Defined Data Structures**
This type of data is not supported by python but can be programmed to reflect the same functionality using concepts supported by python are user-defined data structures.
Data structure that can be implemented this way: Linked list, **Stack, Queue, Tree, Graph and Hashmap**

**Linked list**
It is a linear data structure, in which the elements are not stored at contiguous memory locations.
An example:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/as99ix721h5zr8vtr8lz.png)
**Stack**
It is the linear structure that permits data to be inserted and removed from the same end thus follows a last in first out (LIFO) system. Insertion and removal or deleting is known as _push()_ and _pop()_ respectively.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vu2b8drft4rzf7zumvxd.png)
** Queue**
It is a linear structure that allows insertion of elements from one end and deletion from the other. It follows, First In First Out(FIFO) methodology.  The end is called the front of the queue. The other end is the rear end of the queue. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2wlpdolctkqoq7mmjh3.png)
**Tree**
A tree is a non-linear but hierarchical data structure. Trees are useful for storing data that aren’t linearly connected to each other but form a hierarchy. 
Example:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ibtn0k0jeutch50bhh8l.png)

**Graph**
A Graph is a non-linear data structure consisting of nodes and edges. The nodes called as vertices and the edges are lines or arcs that connect any two nodes in the graph.  A Graph consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of nodes.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7vkraxdkt42fgtnjuew5.png)
**Hashmap**
Hash maps are indexed data structures. They use a hash function to compute an index with a key into an array of buckets or slots. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krsyx3a4v5i04727hljl.png)
**Categories of algorithms in python**
From the data structure point of view, following are some important categories of algorithms
_**Search**_ − Algorithm to search an item in a data structure.
**_Sort_ **− Algorithm to sort items in a certain order.
_**Insert**_ − Algorithm to insert item in a data structure.
**_Update_** − Algorithm to update an existing item in a data structure.
_**Delete**_ − Algorithm to delete an existing item from a data structure.

**Conclusion**
Data Structures help store and organize data in python and algorithms are instructions designed to produce desirable outputs.