List Methods Flashcards
.append(elem)
(list method)
adds an element to the end of a list
returns None (modifies list in place)
.clear()
(list method)
removes all elements from the list
returns None (modifies list in place)
.copy()
(list method)
Returns a copy of the list
.count(elem)
(list method)
returns the number of elements with the specified value in the list
.extend([elem,elem])
(list method)
Add the elements of a list (or any iterable), to the end of the current list
returns None (modifies list in place)
.index(elem)
(list method)
Returns the index of the first element with the specified value
.insert(idx, elem)
(list method)
adds an element to the list at specified index
returns None (modifies list in place)
.pop(idx)
(list method)
removes element from specified position
default value is -1, meaning the last element in the list
Returns the removed element
.remove(elem)
(list method)
removes the first item with the specified value
returns None (modifies list in place)
.reverse()
(list method)
reverses the order of the list
returns None (modifies list in place)
.sort()
(list method)
sorts the list
returns None (modifies list in place)
len(list)
returns the number of elements in a list
List Slicing
list[start:end]
start idx is included in new list
end idx is not included in new list
returns the new list; leaves original list unmodified
sorted(list)
returns a new sorted list
.sort() is faster than sorted()