PYTHON Reference Guide
Revision Time: 4 mins

List Methods Reference

Adding, deleting, and slice transformations on lists.

append
Return: None

Add an element to the end of the list.

Used In: Accumulating results, batch processing.

Syntax signature:list.append(x)
Code snippet:
python
lst = [1, 2]
lst.append(3)
print(lst)
Expected Output:[1, 2, 3]

Time Complexity: O(1) amortized

Common Mistakes: Writing x = lst.append(3) assigns None to x because append() does not return the list.

Related Methods: extend(), insert()

Comparison:

append() vs extend(): append() adds its argument as a single element to the end of the list; extend() iterates over its argument, appending each item to the list.

Remember: Modifies the list in-place.
extend
Return: None

Append elements from an iterable (like another list) to the current list.

Used In: Flattening nested lists, accumulating batch feeds.

Syntax signature:list.extend(iterable)
Code snippet:
python
lst = [1, 2]
lst.extend([3, 4])
print(lst)
Expected Output:[1, 2, 3, 4]

Time Complexity: O(K) where K is elements count in iterable

Related Methods: append()

Comparison:

append() vs extend(): extend() flattens the items of the input list; append() adds the list object itself.

Remember: More memory efficient than joining lists using + since it edits in-place.
insert
Return: None

Insert element at a specific index, shifting subsequent elements.

Used In: Injecting values at controlled positions.

Syntax signature:list.insert(index, x)
Code snippet:
python
lst = [10, 30]
lst.insert(1, 20)
print(lst)
Expected Output:[10, 20, 30]

Time Complexity: O(N)

Related Methods: append()

Remember: Avoid inserts at index 0 on large lists; shifts all items, taking O(N) linear time.
remove
Return: None

Remove the first occurrence of a value from the list.

Used In: Deleting specific entries from collections.

Syntax signature:list.remove(x)
Code snippet:
python
lst = ['a', 'b', 'a']
lst.remove('a')
print(lst)
Expected Output:['b', 'a']

Time Complexity: O(N)

Related Methods: pop()

Remember: Raises ValueError if the item is not present; check with 'if x in list:' first.
pop
Return: T

Remove and return item at index (default last).

Used In: Stack operations, queue consumption.

Syntax signature:list.pop(index=-1)
Code snippet:
python
lst = [10, 20, 30]
val = lst.pop(0)
print(val, lst)
Expected Output:10 [20, 30]

Time Complexity: O(1) for last, O(N) for index 0 (requires shifting elements)

Common Mistakes: Popping from an empty list raises IndexError.

Related Methods: remove(), del

Comparison:

remove() vs pop(): remove(x) searches for the first occurrence of value x and deletes it in O(N) without returning it; pop(i) removes and returns the item at index i in O(N) (or O(1) if last).

Remember: Popping from index 0 is slow; use collections.deque.popleft() for O(1) FIFO queues.
clear
Return: None

Remove all elements from the list, making it empty.

Used In: Emptying batches after processing.

Syntax signature:list.clear()
Code snippet:
python
lst = [1, 2]
lst.clear()
print(lst)
Expected Output:[]

Time Complexity: O(1)

Related Methods: del

Remember: Keeps the same list object reference, saving allocator memory.
copy
Return: List[T]

Return a shallow copy of the list.

Used In: Creating safe isolated lists for sorting.

Syntax signature:list.copy()
Code snippet:
python
lst = [1, [2]]
dup = lst.copy()
dup[1].append(3)
print(lst)
Expected Output:[1, [2, 3]]

Time Complexity: O(N)

Related Methods: copy.deepcopy()

Comparison:

copy() vs copy.deepcopy(): copy() performs a shallow copy; deepcopy() recursively copies all nested elements.

Remember: Since it is a shallow copy, nested mutable objects are still shared references.
sort
Return: None

Sort the elements of the list in-place.

Used In: Ordering datasets, sorting log events.

Syntax signature:list.sort(key=None, reverse=False)
Code snippet:
python
lst = [3, 1, 2]
lst.sort()
print(lst)
Expected Output:[1, 2, 3]

Time Complexity: O(N log N) (Timsort)

Related Methods: sorted()

Comparison:

sort() vs sorted(): sort() modifies the list in-place and returns None; sorted() returns a new sorted list, leaving the original unchanged.

Remember: Use a custom lambda as a key (e.g. key=lambda x: x['sal']) for sorting dictionaries.
reverse
Return: None

Reverse the elements of the list in-place.

Used In: Stack processing.

Syntax signature:list.reverse()
Code snippet:
python
lst = [1, 2, 3]
lst.reverse()
print(lst)
Expected Output:[3, 2, 1]

Time Complexity: O(N)

Related Methods: reversed()

Remember: Modifies the list in-place. Use list[::-1] if you want to create a reversed copy instead.
index
Return: int

Return index of the first occurrence of a value.

Used In: Locating specific offsets.

Syntax signature:list.index(x, start=0, end=len(list))
Code snippet:
python
['a', 'b', 'c'].index('b')
Expected Output:1

Time Complexity: O(N)

Related Methods: find()

Remember: Throws a ValueError if the value is missing.
count
Return: int

Return count of occurrences of a value in the list.

Used In: Frequency checks, quick validation.

Syntax signature:list.count(x)
Code snippet:
python
[1, 2, 1, 1].count(1)
Expected Output:3

Time Complexity: O(N)

Related Methods: collections.Counter

Remember: Iterates the entire list linearly to determine the frequency.