Python 1
Python 1
Data Structures are a way of organizing data so that it can be accessed more efficiently
depending upon the situation. Data Structures are fundamentals of any programming
language around which a program is built. Python helps to learn the fundamental of these
data structures in a simpler way as compared to other programming languages.
Lists
Python Lists are just like the arrays, declared in other languages which is an ordered
collection of data. It is very flexible as the items in a list do not need to be of the same type.
List elements can be accessed by the assigned index. In python starting index of the list,
sequence is 0 and the ending index is (if N elements are there) N-1.
Dictionary
Python dictionary is like hash tables in any other language with the time complexity of
O(1). It is an unordered collection of data values, used to store data values like a map,
which, unlike other Data Types that hold only a single value as an element, Dictionary
holds the key:value pair. Key-value is provided in the dictionary to make it more
optimized.
Tuple
Python Tuple is a collection of Python objects much like a list but Tuples are immutable in
nature i.e. the elements in the tuple cannot be added or removed once created. Just like a
List, a Tuple can also contain elements of various types.
In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or
without the use of parentheses for grouping of the data sequence.
Set
Python Set is an unordered collection of data that is mutable and does not allow any
duplicate element. Sets are basically used to include membership testing and eliminating
duplicate entries. The data structure used in this is Hashing, a popular technique to perform
insertion, deletion, and traversal in O(1) on average.
String
Python Strings are arrays of bytes representing Unicode characters. In simpler terms, a
string is an immutable array of characters. Python does not have a character data type, a
single character is simply a string with a length of 1.
Collections Module
Python collection module was introduced to improve the functionality of the built-in
datatypes. It provides various containers let’s see each one of them in detail.
Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations. A linked list is represented by a pointer to the first node of the linked
list. The first node is called the head.
2) Stack (using - list, queue,LifoQueue).
1) Using list
Stack works on the principle of “Last-in, first-out”. Also, the inbuilt functions in Python
make the code short and simple. To add an item to the top of the list, i.e., to push an item,
we use append() function and to pop out an element we use pop() function. These functions
work quiet efficiently and fast in end operations.
2)Queue
Given a Queue data structure that supports standard operations
like enqueue() and dequeue(). The task is to implement a Stack data structure using only
instances of Queue and Queue operations allowed on the instances
3)LIFO Queue
A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-
In/Last-Out (FILO) manner. 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.
The functions associated with stack are:
empty() – Returns whether the stack is empty – Time Complexity: O(1)
size() – Returns the size of the stack – Time Complexity: O(1)
top() / peek() – Returns a reference to the topmost element of the stack – Time
Complexity: O(1)
push(a) – Inserts the element ‘a’ at the top of the stack – Time Complexity:
O(1)
pop() – Deletes the topmost element of the stack – Time Complexity: O(1)
4) Linked list.
A linked list is a sequence of data elements, which are connected together via links. Each
data element contains a connection to another data element in form of a pointer. Python does
not have linked lists in its standard library. We implement the concept of linked lists using the
concept of nodes as discussed in the previous chapter.
We have already seen how we create a node class and how to traverse the elements of a
node.In this chapter we are going to study the types of linked lists known as singly linked
lists. In this type of data structure there is only one link between any two data elements. We
create such a list and create additional methods to insert, update and remove elements from
the list.
Removing an Item
We can remove an existing node using the key for that node. In the below program we locate
the previous node of the node which is to be deleted.Then, point the next pointer of this node
to the next node of the node to be deleted.