PyLeet Flashcards
How do you get a list of numbers in a particular range?
list(range(0,3))
How do you import Counter?
from collections import Counter
How do you get the highest count item from a Counter?
ctr.most_common()
Initialize a list of “False” n times.
[False] * n
Initialize a two dimensional list of [0] n * m times.
[[0]*n for _ in range(m)]
Where would one initialize a variable upon class instantiation?
def __init__():
How do you loop through two arrays at once, side by side?
for i, j in zip(a, b):
print(i, j)
Diff between sort() and sorted()?
sort() sorts in place, sorted() returns a new list
How does one import components for heap operations?
from heapq import heapify, heappush, heappop
How do you initialize a heap?
heapify(my_list)
How do you add an element to a heap?
heappush(my_list, new_item)
How do you remove the next element from a heap?
heappop(my_list)
How to do integer division (no decimal or remainder)?
a//b
How to get the first index of a character in a string?
s.index(‘e’)
How to get the last index of a character in a string?
str.rfind.(‘e’)
How to get the next index of a character after a given index in a string?
s.index(‘l’, 3)
How do you count the number of occurences of something in a list?
ls.count(1)
How do I replace part of a string?
s.replace(‘earl’, ‘world’)
What are two ways to remove a particular item from a set?
fruits.remove(‘apple’), fruits.discard(‘apple’)
How do you remove the last item from a list?
my_list.pop()
What’s the difference between the two ways to remove an item from a set?
remove throws an error if the item doesn’t exist, discard doesn’t
How do you insert an item into a list at a certain index?
my_list.insert(3, “Apple”)
How to initialize 2 variables at once?
x, y = 0, 0
How do you loop through the index and value of a list at the same time?
for i, v in enumerate(ls):
How do you loop through the key and value of a dictionary?
for k, v in d.items():
What happens if you enumerate a dictionary?
It loops through the index # and the keys, as if it were a list of keys.
What happens if you enumerate a dictionary.values()?
Loop through the list of values with index # and value.
What’s the syntax for sorting a list of lists using sorted?
x = sorted(ls, key=lambda x: x[0]
What’s the syntax for sorting via a custom sort function?
x = sorted(ls, key=lambda x: my_func(x))
What are two ways to reverse a list?
ls.reverse(), ls[::-1]
What is bitwise AND?
a & b
What is bitwise OR?
a | b
What is bitwise XOR?
a ^ b
What is bitwise NOT?
~a
What is bitwise left shift?
a «_space;1
What is bitwise right shift?
a»_space; 1
How do you display the bytes of an integer?
bin(n)[2:]
What’s the difference between remove() and pop()?
remove() removes by value, pop() removes by index. If no argument is passed to pop(), it removes the last item in the list.
What’s the fastest way to tally/ count the number of occurances of something in a list?
from collections import Counter
Counter(myString/myList)
How does one import Counter?
from collections import Counter
How does one update a Counter with an additional list or string?
my_counter.update(new_list_or_string)
How does one access a counter value?
my_counter[key]
How do you loop through items in a Counter?
for k, v in my_counter.items()
How does one get the n most common items from a counter? What is the resultant format?
my_counter.most_common(2) : [(key1, count1), (key2, count2)]