Introduction to Data Structures With Python.
Tags: beginners, python, codenewbie, 100daysofcode
Published: 2022-02-21 21:03:19

### Introduction

**Data structures** allow us to organize, store, and manage data for efficient access and modification.

In this article, we shall cover in-built data structures and user-defined data structures. 


#### In-Built Data Structures

 **1. Lists**
 A list stores multiple items in a single variable. It is defined using square brackets [] and holds data that is separated by commas. The list is mutable, which means we can modify its element after it is created. It is also ordered, which means the items have a defined order, and that order will not change. If you add new items to a list, the new items will be placed at the end of the list. Lists can also contain a mix of different data types. Lists are changeable, meaning that we can change, add, and remove items in a list after it has been created. The first item in a list has index `[0]`, the second item has index `[1]`. Since lists are indexed, lists can have items with the same value.

![image](https://user-images.githubusercontent.com/40733583/155010127-266d2089-d396-4c6d-b8ff-041cb6d65aa6.png)

 **2. Dictionaries**
Dictionaries are used to store data values in ***key:value*** pairs. It is a collection which is ordered and changeable. It is defined using curly braces {} and holds data that is separated by commas. *Values* in a dictionary can be of any data type and can be duplicated, whereas *keys* can’t be repeated and must be _immutable_, which means we can not modify its element after it is created. Note that dictionary keys are case sensitive.
![image](https://user-images.githubusercontent.com/40733583/155011524-cbf6fedb-a9f3-4b14-9690-97c5c98df612.png)

 **3. Tuples**
Tuples are very similar to Lists except that they are immutable. It is defined using round brackets (). Tuple Packing is the creation of a python tuple without the use of parentheses.

![image](https://user-images.githubusercontent.com/40733583/155012569-c4b2794d-4a6f-45d7-b493-364cedffbac1.png)

 **4. Sets**
 A set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. It is defined using curly braces {} or using the function set().
![image](https://user-images.githubusercontent.com/40733583/155013402-1af99e77-b778-4cc7-bc12-3258d51b1373.png)
 
 **User-Defined data structures**

 **1. Queues**

Queue is a linear structure which follows a particular order in which the operations are performed. The order is FIFO which means First In First Out. A good example of a queue, is a queue of shoppers in a supermarket where the shopper that came first to the till is served first. In a queue, we remove the item the least recently added.

**Operations on Queue:**  
Mainly the following four basic operations are performed on queue:  

- **Enqueue:** Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.  
- **Dequeue:** Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.  
- **Front:** Get the front item from queue.  
- **Rear:** Get the last item from queue.

**Implementation**

![image](https://user-images.githubusercontent.com/40733583/155020553-84bc6f47-0048-4d83-96d9-36857b86b63c.png)
 
 **2. Stack**
The stack is a linear data structure where elements are arranged sequentially. It follows the mechanism L.I.F.O which means Last In First Out. For example, in browsers the last opened tab will be the top most item in the history. In stack, a new element is added at one end and an element is removed from that end only. The insert and delete operations are often called push and pop respectively.

**Below are some of the functions associated with stack:**

-   **empty()**  – Returns whether the stack is empty
-   **size()**  – Returns the size of the stack
-   **top()** – Returns a reference to the topmost element of the stack
-   **push()**  – Inserts an element at the top of the stack
-   **pop()**  – Deletes the topmost element of the stack

**Implementation using list**

![image](https://user-images.githubusercontent.com/40733583/155018252-66b7322e-d046-4d69-bf69-99a63c81d7ce.png)
 
 **3. Linked List**
Linked List is a linear data structure where the elements are linked using pointers. Each element of a linked list is called a **node**, and every node has two different fields:
1.  **Data**  contains the value to be stored in the node.
2.  **Next**  contains a address to the next node on the list.

The first node is called the  **`head`**, and it’s used as the starting point for any iteration through the list. The last node must have its  `next`  reference pointing to  `Null` to determine the end of the list.