Python - List Methods Flashcards
Return - none
Args - ( some number / string / list / etc )
___________
adds a single item to the existing list. It doesn’t return a new list; rather it modifies the original list.
list.append(item)
Return - none
Args - ( some list to add to end )
___________
extends the list by adding all items of a list (passed as an argument) to the end.
list1.extend(list2)
Return - none
Args - ( index, value to insert )
___________
inserts an element to the list at a given index.
list.insert(index, element)
Return - none
Args - ( value to insert )
___________
searches for the given element in the list and removes the first matching element.
list.remove(element)
Return - none
Args - ( value to find )
___________
finds the given element in a list and returns its position.
However, if the same element is present more than once, index() method returns its smallest/first position.
If not found, it raises a ValueError exception indicating the element is not in the list.
list.index(element)
Return - none
Args - ( value to count )
___________
counts how many times an element has occurred in a list and returns it.
list.count(element)
Return - none
Args - ( optional index to remove from the list )
___________
takes a single argument (index) and removes the item present at that index.
If the index passed to the pop() method is not in range, it throws IndexError: pop index out of range exception.
The parameter passed to the pop() method is optional. If no parameter is passed, the default index -1 is passed as an argument which returns the last item.
list.pop(index)
Return - none
Args - none
___________
reverses the elements of a given list.
list.reverse()
Return - none
Args - ( optional function that acts as key for the sort comparison, reverse = True/False )
___________
sorts the elements of a given list in a specific order - Ascending or Descending.
list.sort( key= … , reverse= … )
Return - the new sorted list
Args - ( list, optional function that acts as key for the sort comparison, reverse = True/False )
___________
sorts the elements of a given list in a specific order - Ascending or Descending.
sorted( list, key= … , reverse= … )
Return - returns an entirely new list (not a reference!)
Args - none
___________
if you need the original list unchanged when the new list is modified, you can use copy() method. This is called shallow copy.
new_list = list.copy()
~ same as ~
new_list = list[ : ]
~ same as ~
new_list = list( old_list )
Return - none
Args - none
___________
removes all items from the list.
list.clear()
Return - boolean
Args - ( iterable )
___________
returns:
True if at least one element of an iterable is true
False if all elements are false or if an iterable is empty
any(iterable)
Return - boolean
Args - ( iterable )
___________
returns:
True - If all elements in an iterable are true
False - If any element in an iterable is false
all( iterable )
Return - string
Args - ( object like a string, list, etc )
___________
returns a string containing printable representation of an object.
ascii( object )