Python Stl
Python Stl
For C++ STL (Standard Template Library) programmers transitioning to Python, here’s a guide to
understanding Python equivalents for common STL components and idioms:
1. Containers
2. Algorithms
`sorted(iterable, key=lambda x:
`std::stable_sort` Python’s `sorted` is stable by default.
x, stable=True)`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/5
C++ STL Algorithms Python Equivalent Notes
3. Iterators
4. Utility Functions
`std::tuple` Tuple: `(a, b, c)` Python tuples can have any number of elements.
`std::make_pair` `(a, b)` Direct tuple creation is simpler in Python.
5. Functional Utilities
`std::function` Python callable (`lambda`, `def`) Python functions are first-class objects.
`std::bind` `functools.partial` Use `partial` for binding arguments.
Python `for` loops or list Use `for` loops for side effects or
`std::for_each`
comprehensions comprehensions for transformations.
`std::transform` `map(function, iterable)` Applies a function to each element.
6. Random Utilities
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/5
C++ STL Python Equivalent Notes
7. Strings
Python emphasizes simplicity and often has direct methods on containers (e.g., `list.append`,
`dict.get`) instead of requiring separate algorithms. Python's dynamic typing and built-in functionality
make it a highly productive language for STL users familiar with C++ paradigms.
Here’s a short Python example corresponding to some common C++ STL operations:
Containers
python
# std::vector equivalent
my_list = [1, 2, 3, 4]
# std::set equivalent
my_set = {1, 2, 3, 4}
# std::map equivalent
my_dict = {"a": 1, "b": 2, "c": 3}
# std::priority_queue equivalent
import heapq
pq = []
heapq.heappush(pq, 3)
heapq.heappush(pq, 1)
heapq.heappush(pq, 2)
print(heapq.heappop(pq)) # Output: 1 (smallest element)
Algorithms
python
# std::sort equivalent
my_list = [4, 2, 3, 1]
sorted_list = sorted(my_list) # [1, 2, 3, 4]
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/5
my_list.sort() # In-place sort: [1, 2, 3, 4]
# std::reverse equivalent
reversed_list = list(reversed(my_list)) # [4, 3, 2, 1]
# std::find equivalent
if 3 in my_list:
print("Found 3!")
# std::accumulate equivalent
from functools import reduce
my_sum = reduce(lambda x, y: x + y, my_list) # Sum of elements
Iterators
python
# std::distance equivalent
iterator = iter(my_list)
distance = len(list(iterator)) # Length of iterator (consumes it)
Utility Functions
python
# std::pair equivalent
pair = (1, 'a') # Tuple
# std::make_pair equivalent
pair = (2, 'b')
# std::tuple equivalent
triple = (1, 'a', 3.14)
# std::get equivalent
print(triple[1]) # Output: 'a'
Functional Utilities
python
# std::transform equivalent
my_list = [1, 2, 3]
squared = list(map(lambda x: x**2, my_list)) # [1, 4, 9]
These examples show how Python's built-in libraries and syntax can handle many tasks typically
managed with STL in C++.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/5