Python

Python Data Structures

~2 mins read

collections

1
2
3
4
dict
list
set
tuple
1
2
def tree():
    return collections.defaultdict(tree)
1
2
3
4
5
6
7
8
9
10
from enum import Enum

# class syntax
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# functional syntax
Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])
1
2
3
4
5
6
7
8
9
10
from collections import deque

d = deque()

for i in range(10):
    d.append(i)

d.pop() # 9

d.popleft() # 0
1
2
3
4
5
6
7
from heapq import heappush, heappop

def heapsort(iterable):
    h = []
    for value in iterable:
        heappush(h, value)
    return [heappop(h) for i in range(len(h))]

🎰