I Made A Python Cheat Sheet For Data Structures and Algorithms (Useful For Leetcode) by Abdul Malik Medium
I Made A Python Cheat Sheet For Data Structures and Algorithms (Useful For Leetcode) by Abdul Malik Medium
Search
Listen Share
Click here for similar Java Resource since many comments asked (not made by me)
If you’d like to see similar content consider following me to be updated with other such
Cheat Sheets.
Usage Guide
• Keep this guide open beside you while solving problems and take a look as
1 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
• Read through this once just to get an idea of the possibilities with Python.
• (optional) Read this guide every few days or once a week for around 1 month.
You’ll automatically have most syntax in your mind (spaced repetition).
Basics
• Data Types
2 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
Data Structures
Important data structures for Leetcode
Lists
3 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
nums = [1,2,3]
4 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
a[start:stop:step]
is equivalent to:
While the :-based notation is very helpful for simple slicing, the
explicit use of slice() objects simplifies the programmatic
generation of slicing.
Dictionary
Dictionaries are used to store data values in key:value pairs. Info about
collections.Counter() available below.
5 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
dict = {'a':1,'b':2,'c':3}
6 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
in normal dictionary)
# Also, the new element created will be of argument type, for
example in the below line
# an element of type 'list' will be made for a Key that does not
exist
myDictionary = defaultdict(list)
Counter
Python Counter is a container that will hold the count of each of the elements
present in the container. The counter is a sub-class available inside the
dictionary class. Specifically used for element frequencies
# Initialization
Counter(list1) # => Counter({'x': 4, 'y': 2, 'z': 2})
Counter("Welcome to Guru99 Tutorials!") # => Counter({'o': 3, ' ':
3, 'u': 3, 'e': 2.....})
# Updating
counterObject = collections.Counter(list1)
counterObject.keys() = [ 'x' , 'y' , 'z' ]
most_common_element = counterObject.most_common(1) # [('x', 4)]
counterObject.update("some string") # => Counter({'o': 3, 'u': 3,
'e': 2, 's': 2})
counterObject['s'] += 1 # Increase/Decrease frequency
# Accessing
frequency_of_s = counterObject['s']
# Deleting
del couterObject['s']
Deque
7 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
queue = deque(['name','age','DOB'])
Heapq
8 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
As we know the Heap Data Structure is used to implement the Priority Queue
ADT. In python we can directly access a Priority Queue implemented using a
Heap by using the Heapq library/module.
nums = [5, 7, 9, 1, 3]
9 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
# and the returned value from that function is then used to rank
that element in the heap
heapq.nlargest(k, iterable, key = fun)
heapq.nsmallest(k, iterable, key = fun)
Sets
set = {1,2,3}
set.add(item)
set.remove(item)
set.discard(item) | set.remove(item) # removes item | remove will
throw error if item is not there, discard will not
10 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
set.union(anotherSet) # ...
set.update(anotherSet) # adds anotherSet without duplicate
Tuples
• Similar to list
tuple = (1,2,3,1)
Strings
# ** split Function **
#The split() method breaks up a string at the specified separator
and returns a list of strings.
text = 'Python is a fun programming language'
11 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
# ** count Function **
#The count() method returns the number of occurrences of a
substring in the given string.
#Example
message = 'python is popular programming language'
# number of occurrence of 'p'
print('Number of occurrence of p:', message.count('p')) # Output:
Number of occurrence of p: 4
Python integer division behaves differently with -ve numbers ex: -3//2 will give -2
Written by Abdul Malik
answer instead of -1 so always use int(-3/2) for integer division in problems
305 Followers
Resources
Love converting thoughts into code.
12 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
Cheat Sheet
More from AbdulPDF
Malik
Click Here
If you enjoyed this experience consider being a member for more content like
this!
Abdul Malik
20
13 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
Abdul Malik
18
14 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
200 2
Abdul Malik
Recommended
52 from Medium
15 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
6.9K 38
16 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
Ayush Thakur
1.7K 19
Lists
ChatGPT
23 stories · 332 saves
17 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
Rohit Verma
5.1K 31
Naem Azam
11
18 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
12.8K 243
19 of 20 12/22/23, 11:25
I Made A Python Cheat Sheet for Data Structures and... https://buildwithmalik.medium.com/i-made-a-python-c...
4
See more recommendations
20 of 20 12/22/23, 11:25