Felix Olonde

Posted on

# Python:Introduction To Data Structure and Algorithm

Key to learning and mastering any programming language is knowing its data structure and how to write algorithm using that specific language. Python being one of the most popular language in programming arena, mastering its data structure is very important.

Data structures are containers used for organization of data for storage and easy retrieval.
In this article we will discuss some of the most popular and used data structures in python.

1. List: A list is used to store multiple data items in a single variable.
``````fruits = ['Apple','Orange','Guava']
``````

Lists are ordered. This means you can retrieve a list item via an index.

``````result = fruits[1] #Orange
``````

It is worth to note that lists are zero base indexed. This means that the first item in the list is having index zero.

``````result = fruits[0] #Apple
``````

The last item in the list can be retrieved as follows:
`result = fruits[-1]`
Just like string, lists can be sliced.

``````result = fruit[1:2]
``````

Lists are mutable. Meaning you can add and remove items as you desire.

1. Tuples: Unlike lists, Tuples are immutable.This is to say that once a tuple is defined, it cannot be changed. Items in the tuple can be modified but you cannot add or remove an item from the tuple. A tuple is defined as follows:
``````Dimension = (20,30,40)
``````
1. Sets: Sets unlike tuples can not contain duplicates. They are suitable for storing data that should not contain duplicates ie IDs. We define sets as follows:
``````professionals = {'Doctor','Teacher','Engineer'}
``````

Sets are immutable and unordered.
You can get the length of a set using the method `len()`
For example:

``````professionals = {'Doctor','Teacher','Engineer'}
len(professionals ) #3
``````
1. Dictionaries: They store data in key-value pairs. They are ordered(As per version 3.7 and above), changeable and do not allow duplicates.
``````car= {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car)
``````

5.Stack:
stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner.
Stack in Python can be implemented using the following ways:

list
Collections.deque
queue.LifoQueue

1. Queue: Like stack, queue is a linear data structure that stores items in First In First Out (FIFO) manner. With a queue the least recently added item is removed first. A good example of queue is any queue of consumers for a resource where the consumer that came first is served first. Operations associated with queue are:

Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition – Time Complexity : O(1)
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 – Time Complexity : O(1)
Front: Get the front item from queue – Time Complexity : O(1)
Rear: Get the last item from queue – Time Complexity : O(1)