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 )
Return - a counter and an interable
Args - ( sequence/iterable, optional start index )
___________
adds counter to an iterable and returns it (the enumerate object.
The returned object is a enumerate object.
You can convert enumerate objects to list and tuple using list() and tuple() method respectively.
enumerate( iterable, start=0 )
Return - a new iterable that passes the function
Args - ( function, iterable )
___________
filters the given iterable with the help of a function that tests each element in the iterable to be true or not.
filter( function, iterable )
Return - iterator
Args - ( sets / tuples to turn into iterator, optional value that is the end of the sequence )
___________
creates an object which can be iterated one element at a time.
These objects are useful when coupled with loops like for loop, while loop.
iter( object, sentinel )
Return - list
Args - ( opttional set / tuple / list / dict / etc )
___________
constructor creates a list in Python.
The list() constructor returns a mutable sequence list of elements.
If no parameters are passed, it creates an empty list
If iterable is passed as parameter, it creates a list of elements in the iterable
new_list = list( some optional iterable )
~ same as ~
new_list = [ ]
~ same as ~
new_list = list.copy( old list )
Return - length of sequence
Args - ( opttional set / tuple / list / dict / etc )
___________
returns the number of items (length) in an object.
len( some sequence )
Return - largest element in iterable or largest of 2+ parameters
Args - ( optional set / tuple / list / dict / etc )
or ( arg1, arg2, etc )
___________
returns the largest element in an iterable or largest of two or more parameters.
max( )
Return - smallest element in iterable or smallest of 2+ parameters
Args - ( optional set / tuple / list / dict / etc )
or ( arg1, arg2, etc )
___________
returns the smallest element in an iterable or largest of two or more parameters.
min( )
Return - list of mapped results
Args - ( mapping function, iterable to be mapped )
___________
applies a given function to each item of an iterable (list, tuple etc.) and returns a list of the results.
map( function, iterable, … )
Return - reversed list
Args - ( some sequence )
___________
returns the reversed iterator of the given sequence.
*if you don’t want anything returned, just do list.reverse()
reversed( sequence )
Return - sum
Args - ( iterable, optional value to start adding from )
___________
adds the items of an iterable and returns the sum.
sum( iterable, start )
Return - iterator of tuples
Args - ( iterator1, iterator2, etc )
___________
take iterables (can be zero or more), makes iterator that aggregates elements based on the iterables passed, and returns an iterator of tuples.
zipped_object = zip( iterable1, iterable2, etc )
~ or ~
first_values, second_values, etc = zip( iterable1, iterable2, etc )