Data Structures Flashcards

(46 cards)

1
Q

What does the list method append(x) do?

A

Adds x to the end of the list.

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

What does the list method extend(iterable) do?

A

Appends all items from an iterable to the list.

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

What does the list method insert(i, x) do?

A

Inserts x at index i.

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

What does the list method remove(x) do?

A

Removes the first occurrence of x; raises ValueError if not found.

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

What does the list method pop([i]) do?

A

Removes and returns item at index i; removes last item if i not given.

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

What does the list method clear() do?

A

Removes all items from the list.

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

What does the list method index(x[, start[, end]]) do?

A

Returns the first index of x, optionally within start and end.

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

What does the list method count(x) do?

A

Counts how many times x appears in the list.

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

What does the list method sort(key=None, reverse=False) do?

A

Sorts the list in place.

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

What does the list method reverse() do?

A

Reverses the list in place.

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

What does the list method copy() do?

A

Returns a shallow copy of the list.

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

Do mutating list methods return a value?

A

No, they return None.

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

Can lists with mixed incomparable types be sorted?

A

No, lists with incomparable types cannot be sorted.

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

How can a list act as a stack?

A

By using append() to push and pop() to pop.

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

Why aren’t lists ideal as queues?

A

Because pop(0) is inefficient due to element shifting.

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

What should be used for efficient queues?

A

collections.deque for O(1) operations on both ends.

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

What is a list comprehension?

A

A concise way to build a new list from an iterable with optional conditions.

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

How does a nested list comprehension work?

A

It allows an inner comprehension within the expression.

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

What does the del statement do?

A

Deletes an item, slice, or entire variable without returning a value.

20
Q

What is a tuple?

A

An immutable, ordered sequence of values separated by commas.

21
Q

Can tuples contain mutable objects?

A

Yes, tuples can hold mutable objects.

22
Q

How do you define a one-element tuple?

A

With a trailing comma, e.g. (‘item’,)

23
Q

What is tuple packing?

A

Assigning multiple values into a single tuple.

24
Q

What is sequence unpacking?

A

Assigning individual elements of a sequence to separate variables.

25
What is a set in Python?
An unordered collection of unique elements.
26
What are common set operations?
Union, intersection, difference, symmetric difference.
27
How are sets created?
With curly braces or the set() function.
28
Can set comprehensions be used?
Yes, similar to list comprehensions.
29
What is a dictionary in Python?
A collection of key-value pairs with unique, immutable keys.
30
How are dictionaries created?
Using {}, dict(), keyword args, or comprehensions.
31
What happens if you assign a value to an existing key in a dict?
The old value is replaced.
32
What does list(d) return for a dictionary?
A list of keys in insertion order.
33
Can you use dict comprehensions?
Yes, using the format {key: value for ...}.
34
What does dict.items() do?
Returns a view of key-value pairs.
35
What does enumerate() do?
Returns index-value pairs from a sequence.
36
What does zip() do?
Allows iteration over multiple sequences in parallel.
37
What does reversed() do?
Iterates over a sequence in reverse.
38
What does sorted() do?
Returns a sorted version of a sequence.
39
Why avoid modifying a list while iterating?
It can cause unexpected behavior; create a new list instead.
40
What operators can be used in conditions?
in, not in, is, is not, and chained comparisons.
41
What is short-circuit evaluation?
Evaluation stops as soon as the result is determined.
42
What operator allows assignment inside expressions?
The walrus operator :=.
43
How are sequences compared?
Lexicographically, item by item.
44
How are strings compared?
By Unicode code point order.
45
Can mixed numeric types be compared?
Yes, e.g., int with float.
46
What happens when comparing incompatible types?
A TypeError is raised.