Quark's Outlines: Python Built-in Methods
Source: Dev.to
Overview
Python provides built‑in functions like len() and type().
A built‑in method is similar, but it is attached to a built‑in object.
For example, instead of writing append(list, x), you call list.append(x).
Built‑in methods are implemented in C and live inside core types such as list, dict, and str.
When you call a method, Python automatically passes the object as the first argument to the underlying C function.
x = [1, 2]
x.append(3)
print(x)
# prints: [1, 2, 3]
The append() method is part of the list type and mutates the list in place.
Similarly, string methods operate on the string instance:
word = "hello"
print(word.upper())
# prints: HELLO
Here upper() is a method of the string object, not a free function.
Where do Python’s built‑in methods come from?
Built‑in methods were introduced to make core object types more useful. Early versions of Python relied on plain functions for most operations. As the language evolved, types such as list, dict, and str gained their own methods, providing a more natural, object‑oriented interface.
| Year | Milestone |
|---|---|
| 1990 | Functions like len(x) and type(x) were used for most tasks. |
| 1991 | Python 0.9.1 added built‑in types (list, dict, str) with associated methods. |
| 1995 | Python 1.3 expanded string methods (find, replace, split). |
| 2001 | Python 2.2 introduced new‑style classes for all built‑in types, enabling full method support. |
| 2008 | Python 3.0 unified the method system under a consistent object model. |
| 2025 | Modern Python includes dozens of methods across types like set, bytes, and memoryview. |
How to use Python built‑in methods correctly
Adding an item to a list
Problem: You need to add a new item to a list in place.
Solution: Use the list.append() method.
fruits = ['apple', 'banana']
fruits.append('cherry')
print(fruits)
# prints: ['apple', 'banana', 'cherry']
append() mutates the list directly, avoiding the creation of a new list.
Updating a dictionary
Problem: You want to set or change a value for a key safely, handling missing keys gracefully.
Solution: Use the dict.update() method.
scores = {'Alice': 3}
scores.update({'Bob': 5})
print(scores)
# prints: {'Alice': 3, 'Bob': 5}
update() adds the key if it is absent or updates the existing value.
Trimming whitespace from a string
Problem: You need to remove leading and trailing spaces from user input.
Solution: Use the str.strip() method.
name = " Mike "
print(name.strip())
# prints: Mike
strip() leaves interior spaces untouched while removing outer whitespace.
Removing duplicate items from a list while preserving order
Problem: You want to eliminate repeated items from a list, keeping the first occurrence of each element.
Solution: Combine dict.fromkeys() with list().
nums = [1, 2, 2, 3, 1]
unique = list(dict.fromkeys(nums))
print(unique)
# prints: [1, 2, 3]
dict.fromkeys() preserves the order of first appearances; converting back to a list yields the deduplicated result.
Finding a substring within a string
Problem: You need to locate the first occurrence of a character (or substring) in a string, or determine that it is absent.
Solution: Use the str.find() method.
text = "banana"
print(text.find("n"))
# prints: 2
find() returns the index of the first match or -1 if the substring is not found.