Python Data Manipulation Cheat Sheet
·
4 min read
When programming, keeping a solid ground and understanding how to manipulate data will save you a lot of time and effort. For this reason, here are some useful methods to remember, along with examples to clarify when they would be helpful.
Numbers
Method | Result |
min , max(number1, number2, ...) | Returns the smallest/largest number among the provided arguments. For example, min(3, 5) returns 3 , max(3, 5) returns 5 . |
abs(number) | Returns the absolute value of the given number. For example, abs(-7) returns 7 . |
Examples:
Strings
Method | Result |
.lstrip(), .rstrip(), strip(args) | Removes leading/trailing/both whitespace (or specified characters) from a string. By default, whitespace is removed. |
.find(sub) | Returns the lowest index in the string where substring sub is found. Returns -1 if sub is not found. |
.replace(arg1, arg2) | Returns a new string where all occurrences of arg1 are replaced by arg2 . |
.split(sep=None) , .splitlines() | Splits the string at the specified separator (sep ) and returns a list of substrings. .splitlines() splits at line breaks. |
.partition(sep) , .rpartition(sep) | Splits the string at the first/last occurrence of sep |
.endswith(sufix) , .startswith(prefix, start, end) | |
.count(sub) | Number of occurrences of a substring |
Examples:
text = " Hello World "
print(text.strip()) # Output: "Hello World"
print(text.lstrip()) # Output: "Hello World "
print(text.rstrip()) # Output: " Hello World"
sentence = "The quick brown fox jumps over the lazy dog"
print(sentence.find("fox")) # Output: 16
print(sentence.replace("fox", "cat")) # Output: "The quick brown cat jumps over the lazy dog"
lines = "line1\nline2\nline3"
print(lines.splitlines()) # Output: ['line1', 'line2', 'line3']
url = "https://example.com/page"
print(url.startswith("https")) # Output: True
print(url.endswith("page")) # Output: True
print(sentence.count("the")) # Output: 1 (case-sensitive)
Lists, Sets, Tuples
List = []
Tuple = ()
Set = {}
Method | Result | |
.sort() | Sorts the elements of the list in place. By default, it sorts in ascending order. For reverse order, use .sort(reverse=True) . | |
.append(data) | Adds an element to the end of the list. | |
.remove(data) | Removes the first occurrence of data from the list. Raises a ValueError if the element is not found. | |
.pop(index=-1) | Remove an element from a list while also returning it. It accepts one optional input which is the index of the element to remove. If no index is provided, then the last element in the list will be removed and returned. | |
.insert(index, data) | Add an element to a specific index in a list |
Examples:
fruits = ['apple', 'banana', 'cherry']
fruits.sort()
print(fruits) # Output: ['apple', 'banana', 'cherry']
fruits.append('date')
print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']
fruits.remove('banana')
print(fruits) # Output: ['apple', 'cherry', 'date']
last_fruit = fruits.pop()
print(last_fruit) # Output: 'date'
print(fruits) # Output: ['apple', 'cherry']
fruits.insert(1, 'blueberry')
print(fruits) # Output: ['apple', 'blueberry', 'cherry']
Dictionaries
Method | Result | |
dict1.update(dict2) | Updates dict1 with key-value pairs from dict2 . If a key exists in both, the value from dict2 overwrites the one in dict1 . | |
.keys(), .values(), .items() | Return view objects that display a list of keys, values, or key-value pairs respectively. These views update dynamically when the dictionary changes. | |
.get(key, default=None) | Returns the value for key if key is in the dictionary, otherwise returns default (which defaults to None ). | |
.pop(key) | The method takes a key as an argument and removes it from the dictionary. At the same time, it also returns the value that it removes from the dictionary. |
Examples:
# Creating dictionaries
person = {'name': 'Alice', 'age': 30}
updates = {'age': 31, 'city': 'New York'}
# Update dictionary
person.update(updates)
print(person)
# Output: {'name': 'Alice', 'age': 31, 'city': 'New York'}
# Accessing keys, values, and items
print(list(person.keys())) # Output: ['name', 'age', 'city']
print(list(person.values())) # Output: ['Alice', 31, 'New York']
print(list(person.items())) # Output: [('name', 'Alice'), ('age', 31), ('city', 'New York')]
# Using get() with and without default
print(person.get('name')) # Output: Alice
print(person.get('country', 'USA')) # Output: USA
# Pop a key from dictionary
age = person.pop('age')
print(age) # Output: 31
print(person) # Output: {'name': 'Alice', 'city': 'New York'}