Python List Methods Flashcards
append()
Adds an element at the end of the list
list.append(elmnt)
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.append(“orange”)
clear()
The clear() method removes all the elements from a list.
list.clear()
fruits = [‘apple’, ‘banana’, ‘cherry’, ‘orange’]
fruits.clear()
copy()
The copy() method returns a copy of the specified list.
list.copy()
fruits = ['apple', 'banana', 'cherry', 'orange'] x = fruits.**copy**()
count()
The count() method returns the number of elements with the specified value.
list.count(value)
points = [1, 4, 2, 9, 7, 8, 9, 3, 1] x = points.**count**(9)
extend()
The extend() method adds the specified list elements (or any iterable) to the end of the current list.
list.extend(iterable)
fruits = [‘apple’, ‘banana’, ‘cherry’]
points = (1, 4, 5, 9)
fruits.extend(points)
index()
The index() method returns the position at the first occurrence of the specified value.
list.index(elmnt)
fruits = [4, 55, 64, 32, 16, 32] x = fruits.**index**(32)
insert()
The insert() method inserts the specified value at the specified position.
list.insert(pos, elmnt)
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.insert(1, “orange”)
pop()
The pop() method removes the element at the specified position.
list.pop(pos)
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.pop(1)
remove()
The remove() method removes the first occurrence of the element with the specified value.
list.remove(elmnt)
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.remove(“banana”)
reverse()
The reverse() method reverses the sorting order of the elements.
list.reverse()
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.reverse()
sort()
The sort() method sorts the list ascending by default.
list.sort(reverse=True | False, key=myFunc)
cars = [‘Ford’, ‘BMW’, ‘Volvo’]
cars.sort()