Accessing multidimensional lists with an index list

G

Gabriel Birke

Given the multidimensional list l:
l = [ {'v1': 1, 'v2': 2},
[ {'v1':4, 'v2': 7},
{'v1': 9, 'v2': 86},
[ {'v1': 77, 'v2': 88}]
]
]

I want to access specific items the indices of which are stored in
another list. For now, I created a function to do this:

def getNestedValue(l, indices):
while len(indices) > 0:
i = indices.pop(0)
l = l #In future versions, put error checking here
return l

print getNestedValue(l, [1, 2, 0])
print getNestedValue(l, [1, 1])

Is there a more elegant or performant language construct to accomplish
my task?
 
K

Kent Johnson

Gabriel said:
Given the multidimensional list l:
l = [ {'v1': 1, 'v2': 2},
[ {'v1':4, 'v2': 7},
{'v1': 9, 'v2': 86},
[ {'v1': 77, 'v2': 88}]
]
]

I want to access specific items the indices of which are stored in
another list. For now, I created a function to do this:

def getNestedValue(l, indices):
while len(indices) > 0:
i = indices.pop(0)
l = l #In future versions, put error checking here
return l

Is there a more elegant or performant language construct to accomplish
my task?


def getNestedValue(l, indices):
for i in indices:
l = l #In future versions, put error checking here
return l

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top