Unlock the power of Python data structures! A comprehensive cheat sheet for both beginners and experts, enhancing your code proficiency effortlessly. This comprehensive cheat sheet covers all the essential Python data structures, from lists and tuples to dictionaries and sets. Whether you're a beginner or an experienced Python developer, you'll find valuable information in this resource.
Data Structure is a collection of data types and set of rules with a format of organizing, managing and storage which can be used for efficient accessing and modification. Data structures are used in every field for storing and organizing data in the computer.
This Python Data Structure cheat sheet will help you understand what Data Structure is and the basic concepts and commands you must know to get started with it.
Data Structure: It is a way of organizing data that contains the items stored and their relationship to each other
The areas in which Data Structures are applied:
Data structures used in the following areas:
Example: 4, 5, -1 etc
Example: 1.1,2.3,9.3 etc.
Example:
x = ‘Cake’
y = ‘’Cookie’’
Certain operations can be performed on a string:
Example: x*2
Example: Coke
z1 = x[2:]
print(z1)
# Slicing
z2 = y[0] + y[1]
print(z2)
Output: ke
Co
Example:
str.capitalize('cookie')
Example:
str1 = "Cake 4 U"
str2 = "404"
len(str1)
Example:
str1.replace('4 U', str2)
Syntax of writing an array in python:
import array as arr
a = arr.array("I",[3,6,9])
type(a)
Example:
x = [] # Empty list
type(x)
The list can be classified into linear and non-linear data structures
Linear data structures contain Stacks and queues
Non-linear data structures contain Graphs and Trees
Example:
stack.pop() # Bottom -> 1 -> 2 -> 3 -> 4 -> 5 (Top)
stack.pop() # Bottom -> 1 -> 2 -> 3 -> 4 (Top)
print(stack)
An ordered sequence of values indexed by integer numbers. Tuples are immutable
Syntax:
Lists: myList = []
Tuples: myTuple = ()
Syntax:
len(myListOrTuple)
Syntax:
"x" in myListOrTuple
Syntax:
myListOrTuple.index("x") -- If not found, throws a ValueError exception
Syntax:
myListOrTuple.count("x")
Syntax:
Lists: myList[x] = "x"
Tuples: tuples are immutable!
Syntax:
Lists: del myList[x]
Tuples: tuples are immutable!
Lists: myList1 + myList2
Tuples: myTuple1 + myTuple2
Concatenating a List and a Tuple will produce a TypeError exception
Syntax:
Lists: myList.insert(x, "value")
Tuples: tuples are immutable!
Syntax: Lists: myList.append("x")
Tuples: tuples are immutable!
Syntax: List to Tuple: tuple(myList) Tuple to List: list(myTuple)
Set is an unordered collection with no duplicate elements. It supports mathematical operations like union, intersection, difference, and symmetric difference.
Syntax: mySet = set()
Syntax: mySet = set(element1, element2...)
Syntax: mySet.add("x")
Syntax:
Method 1: mySet.remove("x") -- If "x" is not present, raises a KeyErorr
Method 2: mySet.discard("x") -- Removes the element, if present
Syntax: mySet.clear()
Syntax: "x" in mySet
Syntax:
Method 1: mySet1.union(mySet2)
Method 2: mySet1 | mySet2
Syntax:
Method 1: mySet1.intersect(mySet2)
Method 2: mySet1 & mySet2
Syntax:
Method 1: mySet1.difference(mySet2)
Method 2: mySet1 - mySet2
Syntax:
Method 1: mySet1.symmetric_difference(mySet2)
Method 2: mySet1 ^ mySet2
Syntax:
len(mySet)
It is an unordered set of key-value pairs
Syntax:
myDict = {}
Syntax:
myDict["k"] = value
Syntax:
myDict["k"] = newValue
Syntax:
myDict["k"] -- If the key is not present, a KeyError is raised
Syntax:
"k" in myDict
Syntax:
myDict.keys()
Syntax:
len(myDict)
Syntax:
del myDict["k"]
Syntax:
myDict.clear()
Algorithm | Best case | Average case | Worst case | Remarks |
Selection sort | ½ n 2 | ½ n 2 | ½ n 2 | n exchanges, quadratic is the best case |
Insertion sort | n | ¼ n 2 | ½ n 2 | Used for small or partial-sorted arrays |
Bubble sort | n | ½ n 2 | ½ n 2 | Rarely useful, Insertion sort can be used instead |
Shell sort | n log3 n | unknown | c n 3/2 | Tight code, Sub quadratic |
Merge sort | ½ n lg n | n lg n | n lg n | n log n guarantee; stable |
Quick sort | n lg n | 2 n ln n | ½ n 2 | n log n probabilistic guarantee; fastest in practice |
Heapsort | n † | 2 n lg n | 2 n lg n | n log n guarantee; in place |
Worst case | Average case | |||||
Data Structure | Search | Insert | Delete | Search | Insert | Delete |
Sequential search | n | n | n | n | n | n |
Binary search | log n | n | n | log n | n | n |
Binary search tree | n | n | n | log n | log n | sqrt(n) |
Red-black BST | log n | log n | log n | log n | log n | log n |
Hash table | n | n | n | 1 † | 1 † | 1 † |
You can also download the printable PDF of this Data Structure cheat sheet
Source: https://intellipaat.com
#datastructures #algorithms #dsa #python