'Is there a TailMap implementation in Python like in Java?

There is a Java implementation of TreeMap, which contains a method called tailMap as described here: https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html

Is there a corresponding built-in library or third-party library for Python?

As far as I know, this python library implements tree map, but does not implement tailMap -- https://pypi.org/project/sortedcontainers



Solution 1:[1]

As far as I can see, there doesn't exist a built-in library, however there are a few built-in utility functions that can help us to make an quick implementation of it. Here's my quick go at it:

# tailMap.py
x = {'b': 1, 'c': 2, 'e': 3, 'a': 4, 'd': 5}

def tail_map(m, key):
    keys = sorted(m)
    keys = keys[keys.index(key):]
    return {k: m[k] for k in keys}

print(f'{tail_map(x,"c")=}')

This gave me:

> python tailMap.py
> tail_map(x,"c")={'c': 2, 'd': 5, 'e': 3}

Edit: A similar function should be adaptable to the library you referenced.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iHowell