Introduction to Data Structures With Python.
Claire Maina
Posted on February 21, 2022
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.
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.
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.
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().
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
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
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:
- Data contains the value to be stored in the node.
- 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.
Posted on February 21, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 14, 2020