STRING METHODS
1. strip() - Removes leading and trailing spaces
s = " hello world "
print(s.strip()) # Output: "hello world"
2. split() - Splits a string into a list
s = "leet code problems"
print(s.split()) # Output: ['leet', 'code', 'problems']
3. join() - Joins elements of a list into a string
words = ["leet", "code"]
print(" ".join(words)) # Output: "leet code"
4. replace() - Replaces occurrences of a substring
s = "hello world"
print(s.replace("world", "leetcode")) # Output: "hello leetcode"
5. find() / index() - Finds the first occurrence of a substring
s = "leetcode"
print(s.find("code")) # Output: 4
6. startswith() / endswith() - Checks if a string starts/ends with a
substring
s = "leetcode"
print(s.startswith("leet")) # Output: True
print(s.endswith("code")) # Output: True
7. count() - Counts occurrences of a substring
s = "banana"
print(s.count("a")) # Output: 3
8. lower() / upper() - Converts string to lowercase/uppercase
s = "LeetCode"
print(s.lower()) # Output: "leetcode"
print(s.upper()) # Output: "LEETCODE"
9. isalpha() / isdigit() / isalnum() - Checks if the string is alphabetic,
numeric, or alphanumeric
print("abc".isalpha()) # Output: True
print("123".isdigit()) # Output: True
print("abc123".isalnum()) # Output: True
10. [::-1] (Slicing for Reversal) - Used in palindrome problems
s = "racecar"
print(s[::-1]) # Output: "racecar"
11.Count
s = "mississippi"
print(s.count("i", 2, 8)) # Output: 2 (counts "i" from index 2 to 8)
ARRAY METHODS
1. append() → Adds an element to the end of the list
nums = [1, 2, 3]
nums.append(4)
print(nums) # Output: [1, 2, 3, 4]
2. extend() → Merges two lists
nums = [1, 2, 3]
nums.extend([4, 5])
print(nums) # Output: [1, 2, 3, 4, 5]
3. insert() → Inserts an element at a specific index
nums = [1, 2, 4]
nums.insert(2, 3)
print(nums) # Output: [1, 2, 3, 4]
4. pop() → Removes and returns the last element (or at a given index)
nums = [1, 2, 3]
nums.pop()
print(nums) # Output: [1, 2]
5. remove() → Removes the first occurrence of a value
nums = [1, 2, 3, 2]
nums.remove(2)
print(nums) # Output: [1, 3, 2]
6. index() → Finds the first occurrence of a value
nums = [10, 20, 30]
print(nums.index(20)) # Output: 1
7. count() → Counts occurrences of an element
nums = [1, 2, 2, 3, 2]
print(nums.count(2)) # Output: 3
8. sort() → Sorts the list in-place
nums = [3, 1, 2]
nums.sort()
print(nums) # Output: [1, 2, 3]
9. sorted() → Returns a new sorted list
nums = [3, 1, 2]
print(sorted(nums)) # Output: [1, 2, 3]
10. reverse() → Reverses the list in-place
nums = [1, 2, 3]
nums.reverse()
print(nums) # Output: [3, 2, 1]
11. reversed() → Returns a reversed iterator
nums = [1, 2, 3]
print(list(reversed(nums))) # Output: [3, 2, 1]
12. copy() → Creates a shallow copy of the list
nums = [1, 2, 3]
new_nums = nums.copy()
print(new_nums) # Output: [1, 2, 3]
13. clear() → Removes all elements from the list
nums = [1, 2, 3]
nums.clear()
print(nums) # Output: []
14. list comprehension (Alternative to map() and filter())
nums = [1, 2, 3, 4]
squared = [x*x for x in nums]
print(squared) # Output: [1, 4, 9, 16]
HASH MAP
1. get() → Returns the value of a key, or a default if the key is missing
hashmap = {"a": 1, "b": 2}
print(hashmap.get("b")) # Output: 2
print(hashmap.get("c", 0)) # Output: 0 (default value)
2. keys() → Returns all keys
hashmap = {"a": 1, "b": 2, "c": 3}
print(list(hashmap.keys())) # Output: ['a', 'b', 'c']
3. values() → Returns all values
hashmap = {"a": 1, "b": 2, "c": 3}
print(list(hashmap.values())) # Output: [1, 2, 3]
4. items() → Returns key-value pairs
hashmap = {"a": 1, "b": 2, "c": 3}
print(list(hashmap.items())) # Output: [('a', 1), ('b', 2), ('c', 3)]
5. setdefault() → Inserts key with default value if it doesn’t exist
hashmap = {"a": 1, "b": 2}
hashmap.setdefault("c", 3)
print(hashmap) # Output: {'a': 1, 'b': 2, 'c': 3}
6. update() → Updates dictionary with another dictionary or key-value pair
hashmap = {"a": 1, "b": 2}
hashmap.update({"b": 5, "c": 3})
print(hashmap) # Output: {'a': 1, 'b': 5, 'c': 3}
7. pop() → Removes a key and returns its value
hashmap = {"a": 1, "b": 2}
value = hashmap.pop("b")
print(value) # Output: 2
print(hashmap) # Output: {'a': 1}
8. clear() → Removes all key-value pairs
hashmap = {"a": 1, "b": 2}
hashmap.clear()
print(hashmap) # Output: {}
9. copy() → Returns a copy of the dictionary
hashmap = {"a": 1, "b": 2}
new_hashmap = hashmap.copy()
print(new_hashmap) # Output: {'a': 1, 'b': 2}
10. defaultdict() (From collections) → Provides default values for
missing keys
from collections import defaultdict
hashmap = defaultdict(int)
hashmap["a"] += 1
print(hashmap["a"]) # Output: 1
print(hashmap["b"]) # Output: 0 (default int value)