Preview:
# List: Dynamic array, allows duplicate elements
list_example = [1, 2, 3]  # Initialization
list_example.append(4)  # O(1) - Add element to end
list_example.insert(1, 5)  # O(n) - Insert element at index
list_example.pop()  # O(1) - Remove last element
list_example.pop(1)  # O(n) - Remove element at index
element = list_example[2]  # O(1) - Access by index
# Note: Searching for an element is O(n), but not shown here

# Dictionary: Key-Value pairs, allows unique keys
dict_example = {'a': 1, 'b': 2}  # Initialization
dict_example['c'] = 3  # O(1) - Add or update
dict_example.pop('b')  # O(1) - Remove key
value = dict_example['a']  # O(1) - Access by key
# Note: Searching for a key is O(1), but not shown here

# Set: Unordered collection of unique elements
set_example = {1, 2, 3}  # Initialization
set_example.add(4)  # O(1) - Add element
set_example.remove(2)  # O(1) - Remove element
# Note: Checking if an element exists is O(1), but not shown here

# Tuple: Immutable sequence, allows duplicate elements
tuple_example = (1, 2, 3)  # Initialization
element = tuple_example[1]  # O(1) - Access by index
# Note: Searching for an element is O(n), but not shown here

# Heap: (Using heapq module for min heap)
import heapq  # Importing heapq module
heap_example = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]  # Example list
heapq.heapify(heap_example)  # O(n) - Transform list into a heap, in-place
heapq.heappush(heap_example, -5)  # O(log n) - Add element
smallest_element = heapq.heappop(heap_example)  # O(log n) - Pop the smallest item

# Deque: Double-ended queue, allows appending and popping from both ends
from collections import deque  # Importing deque from collections
deque_example = deque([1, 2, 3])  # Initialization
deque_example.append(4)  # O(1) - Add element to the right
deque_example.appendleft(0)  # O(1) - Add element to the left
deque_example.pop()  # O(1) - Remove element from the right
deque_example.popleft()  # O(1) - Remove element from the left
# Note: Access by index is O(n), but faster for operations on ends, not shown here
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter