Table of Contents
Data Structures are a way of organizing data so that it can be accessed more efficiently depending on 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.
Common Data Structures
The various data structures in computer science are divided into two categories as shown below.
Liner Data Structures
Following are the data structures that store the data elements in a sequential manner.
- Array − It is a sequential arrangement of data elements paired with the index of the data element.
- Linked List − Each data element contains a link to another element along with the data present in it.
- Stack − It is a data structure which follows only to specific order of operation. LIFO(last in First Out) or FILO(First in Last Out).
- Queue − It is similar to Stack but the order of operation is only FIFO(First In First Out).
- Matrix − It is two dimensional data structure in which the data element is referred by a pair of indices.
Learn Coding in your Language! Enroll Here!
Non-Liner Data Structures
In these data structures there is no sequential linking of data elements. Any pair or group of data elements can be linked to each other and can be accessed without a strict sequence.
- Binary Tree − It is a data structure where each data element can be connected to maximum two other data elements and it starts with a root node.
- Heap − It is a special case of Tree data structure where the data in the parent node is either strictly greater than/ equal to the child nodes or strictly less than it’s child nodes.
- Hash Table − It is a data structure which is made of arrays associated with each other using a hash function. It retrieves values using keys rather than index from a data element.
- Graph − It is an arrangement of vertices and nodes where some of the nodes are connected to each other through links.
Check out the Entri App for more details on Data Science and Machine Learning!
Specific Data Structures for Python
These data structures are specific to python language and they give greater flexibility in storing different types of data and faster processing in python environment.
- List − It is similar to array with the exception that the data elements can be of different data types. You can have both numeric and string data in a python list.
- Tuple − Tuples are similar to lists but they are immutable which means the values in a tuple cannot be modified they can only be read.
- Dictionary − The dictionary contains Key-value pairs as its data elements.
Preparing for Data Science and Machine Learning? Check out Entri for guidance!
Let us now go through the above mentioned data structures in details.
1. List
It is very flexible as the items in a list do not need to be of the same type.
Example: Python List Operations
# Creating a List with
# the use of multiple values
List = [“Geeks”, “For”, “Geeks”]
print(“\nList containing multiple values: “)
print(List)
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List2 = [[‘Geeks’, ‘For’], [‘Geeks’]]
print(“\nMulti-Dimensional List: “)
print(List2)
# accessing a element from the
# list using index number
print(“Accessing element from the list”)
print(List[0])
print(List[2])
# accessing a element using
# negative indexing
print(“Accessing element using negative indexing”)
# print the last element of list
print(List[-1])
# print the third last element of list
print(List[-3])
Output
List containing multiple values: ['Geeks', 'For', 'Geeks'] Multi-Dimensional List: [['Geeks', 'For'], ['Geeks']] Accessing element from the list Geeks Geeks Accessing element using negative indexing Geeks Geeks
2. Tuple
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.
Example: Python Tuple Operations
# Creating a Tuple with
# the use of Strings
Tuple = (‘Geeks’, ‘For’)
print(“\nTuple with the use of String: “)
print(Tuple)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print(“\nTuple using List: “)
Tuple = tuple(list1)
# Accessing element using indexing
print(“First element of tuple”)
print(Tuple[0])
# Accessing element from last
# negative indexing
print(“\nLast element of tuple”)
print(Tuple[-1])
print(“\nThird last element of tuple”)
print(Tuple[-3])
Output
Tuple with the use of String: ('Geeks', 'For') Tuple using List: First element of tuple 1 Last element of tuple 6 Third last element of tuple 4
3. Dictionary
Indexing of Python Dictionary is done with the help of keys. These are of any hashable type i.e. an object whose can never change like strings, numbers, tuples, etc.
Example: Python Dictionary Operations
# Creating a Dictionary
Dict = {‘Name’: ‘Geeks’, 1: [1, 2, 3, 4]}
print(“Creating Dictionary: “)
print(Dict)
# accessing a element using key
print(“Accessing a element using key:”)
print(Dict[‘Name’])
# accessing a element using get()
# method
print(“Accessing a element using get:”)
print(Dict.get(1))
# creation using Dictionary comprehension
myDict = {x: x**2 for x in [1,2,3,4,5]}
print(myDict)
Output
Creating Dictionary: {'Name': 'Geeks', 1: [1, 2, 3, 4]} Accessing a element using key: Geeks Accessing a element using get: [1, 2, 3, 4] {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Prepare for your Data Science and Machine Learning with Entri!
Entri provides video classes as well on various important topics by the excellent faculties. One will get revision modules, monthly tests based on the classes. Entri provides an excellent platform with full-length mock tests including previous year question papers. It also gives you access to clarify your doubts.