Chapter 2: Data Structures Flashcards

1
Q

What are container sequences? Provide examples.

A

Container sequences hold references to the objects they contain.
e.g list, tuple, collections.deque

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

What are flat sequences? Provide examples.

A

Flat sequences store the value of its contents in its own memory space, not as distinct python objects
e.g str, bytes, array.array
The Python array is a single object, holding a C language array

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

What are mutable sequences? Provide examples

A

Mutable sequences have methods that allow them to be mutated. Mutable sequences (virtually) inherit from MutableSequence, which in turn inherits from Sequences.
e.g list, bytearray, array.array, collections.deque
These methods include insert, append, reverse

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

What are immutable sequences? Provide examples

A

Immutable sequences do not have methods that allow them to be mutated. Mutable sequences (virtually) inherit from Sequence.
e.g tuple, str, bytes

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

What is the Walrus operator?

A

Assigns values to variables as part of a larger expression.
Can also be used during list comprehension to kep the variable in scope with the last value when the comprehension is finished

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

Why may one use a generator expression in the place of a list comprehension?

A

A generator expression saves memory because it yields items one by one instead of building the whole list just to then feed another constructor

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

What are the two key benefits of tuples?

A

Clarity - When a tuple is seen in code the programmer knows its length will never change.

Performance - A tuple uses less memory than a list of the same length. A list is allocated with room to spare to amortize the cost of future appends.

Also a tuple stores an array of references in the tuple struct, a list holds a pointer to the array of references stored elsewhere – this indirection is inefficient.

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

Why is an array of floats more compact than a tuple of floats?

A

An array of floats is a single array object holding the raw value of the floats

A tuple of floats (a container sequence) is a tuple object with references to the float objects contained in it. Each float object has a value field and metadata fields.

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

How could we mutate the contents of a tuple?

A

The contents of a tuple are immutable but that just means the references held by a tuple will always point to the same objects

We can mutate the referenced objects if they are mutable

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

Why do slices and ranges exclude the last item?

A
  • It’s easy to see the length of the slice or range when only the stop position is given
  • It’s easy to compute the length of a slice or range when start and stop are given (stop - start)
  • Easy to split a sequence in two parts at any index x
    my_list[:x] and my_list[x:]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can a string or list be reversed using slicing notation?

A

my_string[::-1]
Remember my_string[a:b:c]
a is start
b is stop
c is stride or step

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

How is multidimensional slicing handling in python?

A

The __getitem__ and __setitem__ special methods recieve the indices in my_list[a, b] as a tuple.

my_list[a, b] calls my_list.__getitem__((a, b))

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

What is the ellipsis (…) in Python?

A

It is a token recognised by the Python parser
It is not used in the Python standard library
Numpy uses it as a shortcut when slicing arrays of multiple dimensions

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

How do the + and * operators work with sequences?

A

e.g a = [10, 20] + [30, 40]
Neither operand is modified
A new sequence of the same type is created as a result of the concatenation

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

How does the += operator work with sequences?

A

+= works by calling the __iadd__ special method and fallback to __add__ if this is not implemented

In the case of mutable sequences, the sequence will be changed in place as __iadd__ effectively calls the extend method

If __iadd__ is not implemented a += b is the same as a = a + b
However a + b is evaluated first, producing a new object, which is bound to a

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

What is the difference between list.sort and the sorted built-in?

A

list.sort methods sorts the list in place, without making a copy, and returns None

16
Q

When would an array be preferred over a list?

A

If a list contains only numbers an array.array if more efficient
- As lean as a C array only holds packed bytes representing floats as oppose to references to float objects
Initialised with a typecode and an iterable object array.array(‘d’, [1, ,3 ,4, 2, 5])
All items must be of the type specified by the typecode

17
Q

How can an array be sorted?

A

As of Python 3.10 there is no array in-place sort method. The array must be rebuilt:

a = array.array(a.typecode, sorted(a))

18
Q

What function can be used to insert into a sorted list?

A

import bisect
bisect.insort(my_list, item)
bisect.insort(my_list, item, key=lambda x: -x)

19
Q

What is memoryview in python?

A

A shared-memory sequence type
Allows memory to be shared between data structures without copying

20
Q

Why would a deque be preferred over a list?

A

Even though python lists have queue methods such as append and pop, inserting and removing the the head of a list is costly as the entire list must be shifted

21
Q

What sorting algorithm is used in sorted and list.sort?

A

Timsort
An adaptive algorithm that switches from insertion to merge sort strategies depending on how ordered the data already is
Worst case O(nlogn)