Lists Flashcards
How to turn a string into a list?
string.split(x)
How to get ‘parakeet’ from all = [birds, pets] when birds = [‘parakeet’, ‘eagle’] and pets = [‘dogs’, ‘cats’]?
all[0][0]
Difference between list.append(x) and list.extend(x)/list +=x?
append puts list inside of x
extend/+= merges the two
How to append to list at position 3?
list.insert(3, x)
How to remove third item from a list?
del list[2]
What is it called when it’s del list[x] instead of something like list.del(x)?
del is called a statement
list.del is called a method
How to remove x from a list?
list.remove(x)
How to both delete and grab x?
list.pop(x)
How to make a first in, last out queue?
list.append(‘x’) > list.pop(‘x’). LIFO, work with newest first
How to make a first in, first out queue?
list.pop(x) where x is an integer. FIFO, work with oldest first
How to count number of times something appeared in a list?
list.count(x)
How to convert to a string?
string.join(list)
It’s a string method not a list method, that’s why the string is first
Difference between the functions sort(list) and sorted(list)?
sort(list) sorts list in place
sorted(list) makes a copy of the list, then sorts that
How to copy a list?
a=[1,2,3]
b = a.copy()
c = list(a)
d = a[:]
Difference between tuple and list?
You can’t change tuples