Python List Methods (Complete Explanation)
🔹 1. append(x)
Purpose: Adds element x at the end of the list.
lst = [1, 2, 3]
[Link](4)
print(lst) # [1, 2, 3, 4]
🔹 2. insert(i, x)
Purpose: Inserts element x at position i.
lst = [1, 3, 4]
[Link](1, 2)
print(lst) # [1, 2, 3, 4]
🔹 3. extend(iterable)
Purpose: Adds all elements of another iterable (list, tuple, etc.) to the list.
lst = [1, 2]
[Link]([3, 4, 5])
print(lst) # [1, 2, 3, 4, 5]
🔹 4. remove(x)
Purpose: Removes the first occurrence of element x.
lst = [1, 2, 3, 2]
[Link](2)
print(lst) # [1, 3, 2]
🔹 5. pop([i])
Purpose: Removes and returns element at index i (default = last).
lst = [10, 20, 30]
print([Link]()) # 30
print(lst) # [10, 20]
print([Link](0)) # 10
🔹 6. clear()
Purpose: Removes all elements from the list (empties it).
lst = [1, 2, 3]
[Link]()
print(lst) # []
🔹 7. index(x, start, end)
Purpose: Returns the index of first occurrence of x.
lst = [10, 20, 30, 20]
print([Link](20)) #1
print([Link](20, 2)) # 3 (search starting from index 2)
🔹 8. count(x)
Purpose: Counts how many times x occurs.
lst = [1, 2, 2, 3, 2]
print([Link](2)) # 3
🔹 9. sort(key=None, reverse=False)
Purpose: Sorts the list in ascending order by default.
nums = [3, 1, 4, 2]
[Link]()
print(nums) # [1, 2, 3, 4]
[Link](reverse=True)
print(nums) # [4, 3, 2, 1]
You can also use a key function:
words = ["apple", "banana", "cherry"]
[Link](key=len) # sort by length
print(words) # ['apple', 'banana', 'cherry']
🔹 10. reverse()
Purpose: Reverses the order of elements in the list.
lst = [1, 2, 3]
[Link]()
print(lst) # [3, 2, 1]
🔹 11. copy()
Purpose: Returns a shallow copy of the list.
a = [1, 2, 3]
b = [Link]()
print(b) # [1, 2, 3]
📘 Extra Functions (not methods, but very useful with lists)
These are built-in Python functions often used with lists:
len(lst) → length of list
max(lst) → maximum element
min(lst) → minimum element
sum(lst) → sum of all elements
nums = [10, 20, 30]
print(len(nums)) # 3
print(max(nums)) # 30
print(min(nums)) # 10
print(sum(nums)) # 60
1. Indexing in Lists
Example list:
nums = [10, 20, 30, 40, 50]
Element 10 20 30 40 50
Positive Index 0 1 2 3 4
Negative Index -5 -4 -3 -2 -1
👉 Positive index starts from the left.
👉 Negative index starts from the right.
🔹 2. General Syntax
list[start:end:step]
start → index where slicing begins (inclusive).
end → index where slicing stops (exclusive).
step → gap (default = 1).
🔹 3. Examples of Slicing
A) Basic Slicing
nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # [20, 30, 40]
B) Omitting Start or End
nums = [1, 2, 3, 4, 5]
print(nums[:3]) # [1, 2, 3] → from beginning
print(nums[2:]) # [3, 4, 5] → till end
print(nums[:]) # [1, 2, 3, 4, 5] → full list
C) Using Step
nums = [1, 2, 3, 4, 5, 6, 7]
print(nums[::2]) # [1, 3, 5, 7] → every 2nd element
print(nums[1::2]) # [2, 4, 6] → every 2nd element starting at index 1
D) Negative Index
nums = [10, 20, 30, 40, 50]
print(nums[-3:]) # [30, 40, 50] → last 3 elements
print(nums[:-2]) # [10, 20, 30] → everything except last 2
E) Reverse List
nums = [1, 2, 3, 4, 5]
print(nums[::-1]) # [5, 4, 3, 2, 1]
F) Slice Backwards with Step
nums = [10, 20, 30, 40, 50]
print(nums[4:1:-1]) # [50, 40, 30]
G) Copy List
nums = [100, 200, 300]
copy_nums = nums[:]
print(copy_nums) # [100, 200, 300]
🔹 4. Visual Diagram
For list:
nums = [10, 20, 30, 40, 50]
Positive Index → 0 1 2 3 4
| | | | |
List Values → 10 20 30 40 50
| | | | |
Negative Index → -5 -4 -3 -2 -1
So:
nums[1:4] → takes indexes 1 to 3 → [20, 30, 40]
nums[-4:-1] → takes indexes -4 to -2 → [20, 30, 40]
nums[::-1] → reverse → [50, 40, 30, 20, 10]
🔹 5. Quick Rules
1. Start index = included ✅
2. End index = excluded ❌
3. If start is empty → begins at 0.
4. If end is empty → goes till end.
5. Step positive → moves forward.
6. Step negative → moves backward.