Lists Flashcards
list basics:
len( ) max( ) min( ) sum( ) in / not in [ ]
list are mutable:
list[1:3] = [“a”, “b”] - overwrite
list operators:
[ ] + [ ]
[ 1,2] * 2
list methods:
a.append( )
a.extend( )
a.insert( 2, “apple”)
a.remove( )
a.pop( )
del list[3]
a.index(“apple” )
a.count(“apple”)
a.sort( reverse = True ) /a.sort(key = str.lower)
fruitlist = [“apple”, “strawberry”, “banana”, “raspberry”, “cherry”, “banana”, “durian”, “blueberry”]
fruitlist.sort( key=lambda x: (len(x),x) )
print( fruitlist )
a.reverse( )
append()
attaches an item at the end of a list. You call the method with the item you wish to add as argument.
extend()
makes a list longer by appending the elements of another list at the end. You call the method with the list of which you want to add the elements as argument.
insert()
allows you to insert an element at a specific position in a list. It is called with two arguments, the first being the index of the location where you wish to insert the new element, and the second the new element itself. To insert an element at the front of the list, you can use index 0. When using this, keep in mind that the index of the elements after your insertion change!
remove()
allows you to remove an element from a list. The element you wish to remove is given as argument. If the element occurs in the list multiple times, only the first occurrence will be removed. If you try to remove an element that is not on the list, a runtime error is generated.
pop()
removes an element from the list, but does so by index. It has one optional argument, which is the index of the element that you wish to remove. If you do not provide that argument, pop() removes the last element from the list. If the index is beyond the boundaries of the list, pop() generates a runtime error.
A major difference with remove() is that pop() actually has a return value, namely the element that gets removed. This allows you to quickly process all the elements of a list, while emptying the list at the same time.
del
is neither a method nor a function, but since it is often metioned in one breath with remove() and pop(), I place it here. del is a keyword that allows you to delete a list element, or list slice, by index. It is similar to pop() in functionality, but does not have a return value. Also, pop() cannot be used on slices. To use del, use the statement del [] or del [:].
index()
returns the index of the first occurrence on the list of the element that is given to index() as argument. A runtime error is generated if the element is not found on the list.
count()
returns an integer that indicates how often the element that is passed to it as an argument occurs in the list.
sort()
sorts the elements of the list, from low to high. If the elements of the list are strings, it does an alphabetical sort. If the elements are numbers, it does a numeric sort. If the elements are mixed, it generates a runtime error, unless certain arguments are given.
Another argument that you can give sort() is a key. You have to provide this argument as .sort( key= ), whereby is a function that takes one argument (the element that is to be sorted) and returns a value that is used as key.
reverse()
simply reverses the elements of the list.
aliasing
lista = listb : they are the same lists
lista = listb[:] :they are different lists, shallow copy, which means that it copies each of the elements of the list with a regular assignment, which entails that the items in the list that are lists themselves become aliases of the items on the original list.
lista = deepcopy( listb ) : deep copy
write a function:
output looks like this:
1 2 3
1 - - O
2 - X -
3 - - -
def display_board( b ): print( " 1 2 3" ) for row in range( 3 ): print( row+1, end=" ") for col in range( 3 ): print( b[row][col], end=" " ) print()
board = [ [”-“, “-“, “-“], [”-“, “-“, “-“], [”-“, “-“, “-“] ]
board[1][1] = “X”
board[0][2] = “O”
display_board( board )
list casting
t1 = ( “apple”, “banana”, “cherry” )
list(t)
list comprehensions
sl = [ x*x for x in range( 1, 26 )]
print( sl )
sl = [ x*x for x in range( 1, 26 ) if x%10 != 5]
print( sl )