Python Basic List Functions Cheat Sheet with Examples
1. append()
Method
The append()
method adds a single item to the end of the list.
Example:
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
Output:
['apple', 'banana', 'cherry']
⚠️ Point:
It only adds one item. To add multiple items, use extend()
.
2. insert()
Method
The insert()
method inserts an element at a specified position in the list.
Syntax:
list.insert(index, item)
Example:
fruits = ["apple", "banana"]
fruits.insert(1, "orange")
print(fruits)
Output:
['apple', 'orange', 'banana']
✅ Tip:
This does not replace the item, it shifts elements to the right.
3. remove()
Method
The remove()
method removes the first matching item from the list.
Example:
fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")
print(fruits)
Output:
['apple', 'cherry', 'banana']
⚠️ Point to Remember:
Only the first occurrence is removed. If the item is not found, it raises a ValueError
.
4. pop()
Method
The pop()
method removes and returns the element at the specified position. If no index is specified, it removes the last item.
Example 1 (default):
fruits = ["apple", "banana", "cherry"]
item = fruits.pop()
print(item)
print(fruits)
Output:
cherry
['apple', 'banana']
Example 2 (with index):
fruits = ["apple", "banana", "cherry"]
item = fruits.pop(1)
print(item)
print(fruits)
Output:
banana
['apple', 'cherry']
5. clear()
Method
The clear()
method removes all elements from the list.
Example:
fruits = ["apple", "banana", "cherry"]
fruits.clear()
print(fruits)
Output:
[]
✅ Tip:
The list still exists in memory but is now empty.
6. count()
Method
The count()
method returns the number of times a specified element appears in the list.
Example:
fruits = ["apple", "banana", "cherry", "banana", "banana"]
print(fruits.count("banana"))
Output:
3
✅ Point:
If the item is not found, it returns 0
.
7. index()
Method
The index()
method returns the index of the first occurrence of the specified element.
Example:
fruits = ["apple", "banana", "cherry", "banana"]
print(fruits.index("banana"))
Output:
1
⚠️ Warning:
If the element is not in the list, ValueError
will be raised.
8. len()
Function
The len()
function returns the number of items in a list (or any iterable).
Example:
fruits = ["apple", "banana", "cherry"]
print(len(fruits))
Output:
3
✅ Tip:
Also works with strings, dictionaries, tuples, etc.
9. in
Operator
The in
keyword is used to check if an element exists in a list or any other iterable.
Example:
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("mango" in fruits)
Output:
True
False
✅ Tip:
Used commonly in if
conditions to check for membership.
10. list()
Constructor
The list()
function is used to create a new list from an iterable (like string, tuple, or set).
Example 1: From string
text = "hello"
letters = list(text)
print(letters)
Output:
['h', 'e', 'l', 'l', 'o']
Example 2: From tuple
t = (1, 2, 3)
lst = list(t)
print(lst)
Output:
[1, 2, 3]
✅ Point:
Useful for type conversion from other iterables to lists.
📚 Practice Questions:
📚 Related Topics:
- ➤ Python Arithmetic Operators
- ➤ Basic String Functions
- ➤ Advanced String Functions
- ➤ Basic List Functions
- ➤ Advanced List Functions : Part-1
- ➤ Advanced List Functions : Part-2
- ➤ Basic Tuple Functions
- ➤ Advanced Tuple Functions
- ➤ Basic Dictionary Functions
- ➤ Advanced Dictionary Functions
- ➤ Conditional Statements : if-elif-else
- ➤ Python 'for' Loop
- ➤ Python 'while' Loop
- ➤ Difference between 'for' loop and 'while' loop
- ➤ Introducing Python Functions
📌 Bookmark this blog or follow for updates!
👍 Liked this post? Share it with friends or leave a comment below!