Nginx cache path function I have an MD5 hash string:

_hash = '743a0e0c5c657d3295d347a1f0a66109'


I want to write function that splits passed string to path in nginx cache format:

print hash2path(_hash, [1, 2, 3])
# outputs '7/43/a0e/743a0e0c5c657d3295d347a1f0a66109'


This is my variant:

def hash2path(_hash, levels):
    hash_copy = _hash
    parts = []
    for level in levels:
        parts.append(_hash[:level])
        _hash = _hash[level:]
    parts.append(hash_copy)  
    return '/'.join(parts)


How could this be improved?
Why you can't just hardcode cache path calculation?

&gt;&gt;&gt; _hash = '743a0e0c5c657d3295d347a1f0a66109'
&gt;&gt;&gt; def hash2path(_hash):
...     return "%s/%s/%s/%s" % (_hash[0], _hash[1:3], _hash[3:6], _hash)
... 
&gt;&gt;&gt; hash2path(_hash)
'7/43/a0e/743a0e0c5c657d3295d347a1f0a66109'


UPDATE: If you really need to pass level array there is a little bit faster version:

def hash2path(_hash, levels):
    prev = 0
    parts = []
    for level in levels:
        parts.append(_hash[prev:prev + level])
        prev += level
    parts.append(_hash)
    return "/".join(parts)