You are on page 1of 1

DATA STRUCTURES

Average
Lists and Tuples in Python Algorithm Best case
case
Worst case Remarks Dictionaries
Selection n exchanges,
Ordered sequence of values indexed by integer numbers. Tuples are immutable ½n2 ½n2 ½n2 It is an unordered set of key value pairs
sort quadratic is the best case

CHEAT SHEET
• To specify size of tuple/list: • Initialize an empty Dict
Insertion Used for small or partial-
• To initialize empty list /tuple: n ¼n2 ½n2 Syntax: myDict = {}
Synatx: len(myListOrTuple) sort sorted arrays
Syntax: Lists: myList = [] • Add an element with key "k" to the Dict
• Remove element in position X of list/tuple: Rarely useful,
Tuples: myTuple = () Bubble Syntax: myDict["k"] = value
n ½n2 ½n2 Insertion sort can be used
• To get an element in position x in list/tuple: Syntax: Lists: del myList[x] sort • Update the element with key "k"

Python - Data Structure Syntax: "x" in myListOrTuple



Tuples: tuples are immutable!
Concatenate two lists/tuples:
instead
Tight code,

Syntax: myDict["k"] = newValue
Get element with key "k"
Shell sort n log3 n unknown c n 3/2
• Index of element ‘X’ of list/tuple Sub quadratic
Lists: myList1 + myList2 Syntax: myDict["k"] -- If the key is not
Syntax: myListOrTuple.index("x") - Merge n log n guarantee; present, a KeyError is raised
Tuples: myTuple1 + myTuple2 ½ n lg n n lg n n lg n
Data Types - If not found, throws a ValueError
Concatenating a List and a Tuple will
sort stable • Check if the dictionary has key "k"
exception n log n probabilistic
produce a TypeError exception Syntax: "k" in myDict
It is a way of organizing data that contains the items stored and their Quick sort n lg n 2 n ln n ½n2 guarantee;
• Number of occurance of X in list/tuple: • Get the list of keys
relationship to each other • Insert element in position x of a list/tuple fastest in practice
Syntax: myListOrTuple.count("x") Syntax: myDict.keys()
The areas in which Data Structures are applied: Syntax: Lists: myList.insert(x, n log n guarantee;
Heap sort n† 2 n lg n 2 n lg n • Get the size of the dictionary
• Compiler design Data structures can be used in • Update an item of List/tuple: "value") in place
Syntax: len(myDict)
• Operating system the following areas: Syntax: Lists: myList[x] = "x“ Tuples: tuples are immutable!
Worst Case Average Case • Delete element with key "k" from the dictionary
• Database Management System • RDBMS: Array ( Array of • Append "x" to a list/tuple:
Tuples: tuples are immutable! Syntax: del myDict["k"]
structure) Data Structure Search Insert Delete Search Insert Delete
• Statistical Analysis Package Syntax: Lists: myList.append("x")
• Remove element in position X of list/tuple: • Delete all the elements in the dictionary
• Numerical Analysis • Network data model: Tuples: tuples are immutable!
Sequential
n n n n n n Syntax: myDict.clear()
Graph Syntax: Lists: del myList[x] search
• Graphics • Convert a list/tuple to tuple/list:
• Hierarchical Data model: Tuples: tuples are immutable! Binary search log n n n log n n n
• Artificial Intelligence Syntax: List to Tuple: tuple(myList)
• Simulations Trees Binary search
Tuple to List: list(myTuple) n n n log n log n sqrt(n)
tree Data Structures
Red-black BST log n log n log n log n log n log n
Types of Data Structures Hash table n n n 1† 1† 1†
Primitive Data Structures: Non- Primitive Data Structures: 1 † - Uniform hashing assumption
• Array: It is a compact way of collecting data types where all entries must be of the same Non -
• Integer: It is used to represent numeric data, more specifically whole numbers Primitive
from negative infinity to infinity. Eg: 4, 5, -1 etc
data type. Sets Primitive
Syntax of writing an array in python:
• Float: It stands for floating point number. Eg: 1.1,2.3,9.3 etc import array as arr It is an unordered collection with no duplicate elements. It supports mathematical operations like
• String: It is a collection of Alphabets, words or other characters. In python it a = arr.array("I",[3,6,9]) union, intersection, difference and symmetric difference.
type(a) •
can be created by using a pair of single or double quotes for the sequence. • To initialize an empty set: Union of two sets Integer Float String Boolean
• Linked list: List in Python is used to store collection of heterogeneous items. It is
Syntax: mySet = set() Syntax:
Eg: x = 'Cake’ described using the square brackets [] and hold elements separated by comma
Method 1: mySet1.union(mySet2)
y = '’Cookie’’ Eg: x = [] # Empty list • Initialize a non empty set
Method 2: mySet1 | mySet2 Array List Tuple Dictionary Set File
type(x) Syntax: mySet = set(element1,
Certain operations can be performed on a string:
o The list can be classified into linear and non-linear data structures • Intersection of two sets
element2...)
o Linear data structures contain Stacks and queues Syntax:
o We can use * to repeat the string for o To capitalize the strings • To add element X to the set
a specific number of times. Eg: x*2 o Non-linear data structures contains Graphs and Trees Method 1:
Eg: str.capitalize('cookie') Syntax: mySet.add("x") Linear Non - Linear
• Stack: It is a container of objects that can be inserted or removed according to LIFO(Last mySet1.intersect(mySet2)
o String can be sliced, that is to select
o To retrieve the length of the strings In First Out) concept. pop() method is used during disposal in Python • Remove element "x" from a set: Method 2: mySet1 & mySet2
parts of the string. Eg: Coke Eg: stack.pop() # Bottom -> 1 -> 2 -> 3 -> 4 -> 5 (Top)
Eg: Syntax: • Difference of two sets
z1 = x[2:] stack.pop() # Bottom -> 1 -> 2 -> 3 -> 4 (Top) Method 1: mySet.remove("x") -- Syntax: Stacks Queues Graphs
str1 = "Cake 4 U" print(stack) Trees
print(z1) If "x" is not present, raises a Method 1:
• Queue: It is a container of objects that can be inserted or removed according to
str2 = "404" KeyErorr mySet1.difference(mySet2)
# Slicing FIFO(First In First Out) concept.
len(str1) Method 2: mySet.discard("x") -- Method 2: mySet1 - mySet2
• Graph: It is a data structure that consists of a finite set of vertices called nodes, and a
z2 = y[0] + y[1]
finite set of ordered pair (u,v) called edges. It can be classified as direction and weight Removes the element, if present • Symmetric difference of two sets
o To replace parts of a string with
print(z2) • Binary Tree: Tree is a hierarchical data structure. Here each node has at most two • Remove every element from the set Syntax:
another string
children Syntax: mySet.clear() Method 1:
Output: ke
o Eg: str1.replace('4 U', • Binary Search Tree: It provides moderate access/ search and moderate insertion/ • Check if "x" is in the set mySet1.symmetric_difference(m
Co str2) deletion Syntax: "x" in mySet ySet2)
• Boolean: It is a built-in data type that can take the values TRUE or FALSE • Heap: It is a complete tree and is suitable to be stored in an array, It is either MIN or Max • Size of the sets:
• Hashing: Collection of items that are stored in a way that it becomes easy to find them is
Method 2: mySet1 ^ mySet2
FURTHERMORE:
Syntax: len(mySet)
hashing Data Structures Certification Training Course

You might also like