Python Snippets Flashcards

(57 cards)

1
Q

Python Snippet

A

Explanation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

if not nums:

A

Check if the list is empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

if x in my_set:

A

Check if ‘x’ is an element in the set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

for i, val in enumerate(arr):

A

Loop with both index and value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

nums = [x for x in arr if x % 2 == 0]

A

List of even numbers from ‘arr’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

return sorted(arr)

A

Return a new sorted list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

arr.sort()

A

Sort the list in-place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

from collections import Counter

A

Import Counter to count elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Counter(arr)

A

Create frequency dictionary of ‘arr’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

’‘.join(chars)

A

Join list of characters into a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

list(map(int, input().split()))

A

Read integers from input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

while stack:

A

Loop while stack is not empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

stack.append(x)

A

Push ‘x’ onto the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

stack.pop()

A

Pop the top element from the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

if not stack:

A

Check if stack is empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

set1 & set2

A

Intersection of two sets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

set1 | set2

A

Union of two sets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

set1 - set2

A

Elements in set1 but not in set2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

nums[::-1]

A

Reverse a list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

s[::-1]

A

Reverse a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

any(x > 10 for x in arr)

A

Check if any element is > 10.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

all(x > 0 for x in arr)

A

Check if all elements are > 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

{i: 0 for i in range(5)}

A

Create a dictionary with values 0.

24
Q

for key in my_dict:

A

Loop through dictionary keys.

25
my_dict.get(k, 0)
Get value for key with default 0.
26
try: ... except:
Handle exceptions safely.
27
import heapq
Import heapq module.
28
heapq.heappush(heap, x)
Add element to heap.
29
heapq.heappop(heap)
Remove smallest element.
30
heapq.heapify(arr)
Turn list into a heap.
31
defaultdict(list)
Dictionary with list as default.
32
isinstance(x, list)
Check if 'x' is a list.
33
lambda x: x**2
Short function that squares x.
34
sorted(arr, key=len)
Sort by length of elements.
35
sorted(arr, reverse=True)
Sort in descending order.
36
filter(lambda x: x > 0, arr)
Keep only positive elements.
37
map(str, nums)
Convert numbers to strings.
38
zip(list1, list2)
Pair elements from two lists.
39
list(zip(*matrix))
Transpose a matrix.
40
a, b = b, a
Swap values of a and b.
41
x = a or b
Use 'a' if truthy, else 'b'.
42
x = a and b
Use 'b' if 'a' is truthy, else 'a'.
43
my_dict.items()
Key-value pairs from dict.
44
my_dict.keys()
Keys of the dictionary.
45
my_dict.values()
Values of the dictionary.
46
with open('file.txt') as f:
Open file and close it automatically.
47
yield x
Return value from generator.
48
def gen(): yield from range(5)
Generator that yields 0–4.
49
re.findall(r'\d+', s)
Find all numbers in string.
50
import re
Import regex module.
51
re.sub(r'\s+', ' ', s)
Replace multiple spaces with one.
52
s.strip()
Trim whitespace.
53
s.lower()
Convert string to lowercase.
54
s.upper()
Convert string to uppercase.
55
s.startswith('abc')
Check if string starts with 'abc'.
56
s.endswith('xyz')
Check if string ends with 'xyz'.
57
len(set(s)) == len(s)
Check if all characters are unique.