Python List Methods

Master the essential methods for manipulating and working with Python lists efficiently.

append()
Adds an element at the end of the list
list.append(elmnt)
Essential

append()

Adds an element at the end of the list

Essential

Example

Add an element to the animals list:

animals = ['cat', 'dog', 'rabbit']
animals.append('fish')

print(animals)
# Output: ['cat', 'dog', 'rabbit', 'fish']

Definition and Usage

The append() method appends an element to the end of the list.

Syntax

list.append(elmnt)

Parameter Values

Parameter Description
elmnt Required An element of any type (string, number, object etc.)
clear()
Removes all elements from the list
list.clear()
Essential

clear()

Removes all elements from the list

Essential

Example

Remove all elements from the animals list:

animals = ['cat', 'dog', 'rabbit']
animals.clear()

print(animals)
# Output: []

Definition and Usage

The clear() method removes all elements from the list.

Syntax

list.clear()

Parameter Values

Parameter Description
No parameters
copy()
Returns a copy of the list
list.copy()
Essential

copy()

Returns a copy of the list

Essential

Example

Create a copy of the animals list:

animals = ['cat', 'dog', 'rabbit']
new_animals = animals.copy()

print(new_animals)
# Output: ['cat', 'dog', 'rabbit']

Definition and Usage

The copy() method returns a copy of the list.

Syntax

list.copy()

Parameter Values

Parameter Description
No parameters
count()
Returns the number of elements with the specified value
list.count(value)
Essential

count()

Returns the number of elements with the specified value

Essential

Example

Count how many times 'cat' appears in the animals list:

animals = ['cat', 'dog', 'rabbit', 'cat', 'fish']
x = animals.count('cat')

print(x)
# Output: 2

Definition and Usage

The count() method returns the number of elements with the specified value.

Syntax

list.count(value)

Parameter Values

Parameter Description
value Required Any type (string, number, list, etc.). The value to search for
extend()
Adds all elements from an iterable to the end of the list
list.extend(iterable)
Essential

extend()

Adds all elements from an iterable to the end of the list

Essential

Example

Add elements from tropical list to the fruits list:

fruits = ['apple', 'banana', 'cherry']
tropical = ['mango', 'pineapple', 'papaya']

fruits.extend(tropical)

print(fruits)
# Output: ['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']

Definition and Usage

The extend() method adds all elements from an iterable (list, tuple, string, etc.) to the end of the current list. This modifies the original list in place.

Syntax

list.extend(iterable)

Parameter Values

Parameter Description
iterable Required Any iterable (list, tuple, set, dictionary, string, etc.). The elements to add to the list
index()
Returns the index of the first element with the specified value
list.index(value)
Essential

index()

Returns the index of the first element with the specified value

Essential

Example

Find the position of the value 'cherry' in the fruits list:

fruits = ['apple', 'banana', 'cherry', 'orange']
x = fruits.index('cherry')

print(x)
# Output: 2

Definition and Usage

The index() method returns the position of the first occurrence of the specified value. If the value is not found, it raises a ValueError exception.

Syntax

list.index(elmnt, start, end)

Parameter Values

Parameter Description
elmnt Required Any type. The value to search for
start Optional. An integer specifying at which position to start the search. Default is 0
end Optional. An integer specifying at which position to end the search. Default is the end of the list
insert()
Adds an element at the specified position
list.insert(position, element)
Essential

insert()

Adds an element at the specified position

Essential

Example

Insert the value 'orange' at position 1 in the fruits list:

fruits = ['apple', 'banana', 'cherry']

fruits.insert(1, 'orange')

print(fruits)
# Output: ['apple', 'orange', 'banana', 'cherry']

Definition and Usage

The insert() method inserts an element at the specified position in the list. Existing elements are shifted to the right to make room for the new element.

Syntax

list.insert(position, element)

Parameter Values

Parameter Description
position Required A number specifying the position where the element should be inserted
element Required Any type (string, number, object, etc.). The element to insert
pop()
Removes and returns the element at the specified position
list.pop(position)
Essential

pop()

Removes and returns the element at the specified position

Essential

Example

Remove and return the element at position 1 from the animals list:

animals = ['cat', 'dog', 'rabbit', 'hamster']
x = animals.pop(1)

print(x)
# Output: dog

print(animals)
# Output: ['cat', 'rabbit', 'hamster']

Definition and Usage

The pop() method removes the element at the specified position and returns it. If no position is specified, it removes and returns the last element in the list.

Syntax

list.pop(position)

Parameter Values

Parameter Description
position Optional. A number specifying the position of the element to remove. Default is -1 (the last element)
remove()
Removes the first occurrence of the specified value
list.remove(value)
Essential

remove()

Removes the first occurrence of the specified value

Essential

Example

Remove 'rabbit' from the animals list:

animals = ['cat', 'dog', 'rabbit', 'hamster']

animals.remove('rabbit')

print(animals)
# Output: ['cat', 'dog', 'hamster']

Definition and Usage

The remove() method removes the first occurrence of the element with the specified value. If the value is not found, it raises a ValueError exception.

Syntax

list.remove(value)

Parameter Values

Parameter Description
value Required Any type. The value to remove from the list
reverse()
Reverses the order of the list
list.reverse()
Essential

reverse()

Reverses the order of the list

Essential

Example

Reverse the order of the animals list:

animals = ['cat', 'dog', 'rabbit', 'hamster']

animals.reverse()

print(animals)
# Output: ['hamster', 'rabbit', 'dog', 'cat']

Definition and Usage

The reverse() method reverses the order of the elements in the list. This modifies the original list in place and does not return a new list.

Syntax

list.reverse()

Parameter Values

Parameter Description
No parameters
sort()
Sorts the list in ascending order
list.sort()
Essential

sort()

Sorts the list in ascending order

Essential

Example 1: Basic Sort

Sort the animals list alphabetically:

animals = ['dog', 'cat', 'rabbit', 'hamster']

animals.sort()

print(animals)
# Output: ['cat', 'dog', 'hamster', 'rabbit']

Example 2: Sort in Descending Order

Sort the animals list in reverse alphabetical order:

animals = ['dog', 'cat', 'rabbit', 'hamster']

animals.sort(reverse=True)

print(animals)
# Output: ['rabbit', 'hamster', 'dog', 'cat']

Example 3: Sort by Length

Sort the animals list by the length of each string:

animals = ['dog', 'cat', 'rabbit', 'hamster']

animals.sort(key=len)

print(animals)
# Output: ['cat', 'dog', 'rabbit', 'hamster']

Example 4: Sort by Last Letter

Sort the animals list by the last letter of each string:

def last_letter(s):
    return s[-1]

animals = ['dog', 'cat', 'rabbit', 'hamster']

animals.sort(key=last_letter)

print(animals)
# Output: ['rabbit', 'hamster', 'cat', 'dog']

Example 5: Sort Dictionaries by Year

Sort a list of dictionaries by year:

animals = [
    {'name': 'cat', 'year': 2020},
    {'name': 'dog', 'year': 2018},
    {'name': 'rabbit', 'year': 2022}
]

animals.sort(key=lambda x: x['year'])

print(animals)
# Output: [{'name': 'dog', 'year': 2018}, {'name': 'cat', 'year': 2020}, {'name': 'rabbit', 'year': 2022}]

Example 6: Sort by Age

Sort a list of tuples by age:

animals = [('cat', 5), ('dog', 3), ('rabbit', 2), ('hamster', 4)]

animals.sort(key=lambda x: x[1])

print(animals)
# Output: [('rabbit', 2), ('dog', 3), ('hamster', 4), ('cat', 5)]

Example 7: Case-Insensitive Sort

Sort strings in a case-insensitive manner:

animals = ['Dog', 'cat', 'Rabbit', 'hamster']

animals.sort(key=str.lower)

print(animals)
# Output: ['cat', 'Dog', 'hamster', 'Rabbit']

Example 8: Sort Numbers by Absolute Value

Sort numbers by their absolute value:

numbers = [5, -3, 8, -1, 2]

numbers.sort(key=abs)

print(numbers)
# Output: [-1, 2, -3, 5, 8]

Definition and Usage

The sort() method sorts the list in ascending order by default. This modifies the original list in place and does not return a new list. You can customize the sorting behavior using the reverse and key parameters.

Syntax

list.sort(reverse=True|False, key=myFunc)

Parameter Values

Parameter Description
reverse Optional. A Boolean value. False will sort ascending (default), True will sort descending
key Optional. A function to specify the sorting criteria (e.g., len, str.lower, abs, or a custom function)