Python Collections & String Methods Guide
This document covers commonly used methods and functions associated with Lists, Tuples, Sets, Dictionaries, and
Strings in Python along with syntax and examples.
List
Declaration: nums = [1, 2, 3]
Method Purpose Example
append(x) Adds element to end [Link](4)
insert(i, x) Insert at index [Link](1, 10)
remove(x) Remove first occurrence [Link](2)
pop() Remove last element [Link]()
sort() Sort list [Link]()
reverse() Reverse list [Link]()
count(x) Count occurrences [Link](1)
index(x) Get index [Link](3)
Tuple
Declaration: t = (1, 2, 3)
Method Purpose Example
count(x) Count occurrences [Link](1)
index(x) Get index [Link](2)
Set
Declaration: s = {1, 2, 3}
Method Purpose Example
add(x) Add element [Link](4)
remove(x) Remove element [Link](2)
discard(x) Remove safely [Link](5)
union() Combine sets [Link]({4,5})
intersection() Common elements [Link]({2,3})
difference() Difference between sets [Link]({1})
clear() Remove all elements [Link]()
Dictionary
Declaration: d = {'a': 1, 'b': 2}
Method Purpose Example
get(key) Get value safely [Link]('a')
items() Get key-value pairs [Link]()
keys() Get all keys [Link]()
values() Get all values [Link]()
update() Update dictionary [Link]({'c':3})
pop(key) Remove key [Link]('a')
setdefault() Add default value [Link]('x',0)
String
Declaration: s = 'hello world'
Method Purpose Example
lower() Convert to lowercase [Link]()
upper() Convert to uppercase [Link]()
replace(a,b) Replace text [Link]('o','x')
split() Convert to list [Link]()
join() Join list into string '-'.join(['a','b'])
strip() Remove spaces [Link]()
isalnum() Check alphanumeric [Link]()
isalpha() Check only alphabets [Link]()
isdigit() Check digits [Link]()
startswith() Check prefix [Link]('he')
endswith() Check suffix [Link]('ld')