Skip to content

Instantly share code, notes, and snippets.

@Guilouf
Created January 31, 2019 17:45
Show Gist options
  • Save Guilouf/5631107354af6dc1af449572d388c0f3 to your computer and use it in GitHub Desktop.
Save Guilouf/5631107354af6dc1af449572d388c0f3 to your computer and use it in GitHub Desktop.
Access list dict pass via a tuple of indexes
def search_index(obj, itero):
try:
return search_index(obj[next(itero)], itero)
except StopIteration:
return obj
a = [{"name":"someone", "age":23}]
s = (0, 'name')
result = search_index(a, iter(s))
# >>> result = 'someone'
#If you want to convert "[0]['name']" to (0, 'name') you can try this:
import re
reg = re.findall(r'\[(\d*?)\]|\[\'(\w*?)\'\]', "[0]['name']")
s = [int(index[0]) if not index[1] else index[1] for index in reg]
# [0, 'name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment