Collections Flashcards

1
Q

what is Counters?

A

A Counter is a subclass of dict. Therefore it is an unordered collection where elements and their respective count are stored as a dictionary

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

what is OrderedDict?

A

An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict() and OrderedDict() is that

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

can we use OrderedDict as a stack?

A

Ordered Dict can be used as a stack with the help of popitem function. Try implementing LRU cache with Ordered Dict.

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

what is the diffrence between Defaultdict and dictanaries?

A

Defaultdict is a container like dictionaries present in the module collections. Defaultdict is a sub-class of the dict class that returns a dictionary-like object. The functionality of both dictionaries and defualtdict are almost same except for the fact that defualtdict never raises a KeyError. It provides a default value for the key that does not exists.

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

what is ChainMap

A

Python contains a container called “ChainMap” which encapsulates many dictionaries into one unit. ChainMap is member of module “collections“.

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

what is NAmedtuple?

A

Python supports a type of container like dictionaries called “namedtuple()” present in module, “collections“. Like dictionaries they contain keys that are hashed to a particular value. But on contrary, it supports both access from key value and iteration, the functionality that dictionaries lack.

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

what is deque

A

Deque (Doubly Ended Queue) in Python is implemented using the module “collections“. Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.

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

what is head queue?

A

Heap data structure is mainly used to represent a priority queue. In Python, it is available using “heapq” module. The property of this data structure in Python is that each time the smallest of heap element is popped(min heap). Whenever elements are pushed or popped, heap structure in maintained. The heap[0] element also returns the smallest element each time.

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