PyLeet Flashcards
(118 cards)
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):