Menu

gitpiper

get python Code Snippet in 2022

dictionarylistintermediate

Last Updated: 27 November 2022

Retrieves the value of the nested key indicated by the given selector list from a dictionary or list.

  • Use functools.reduce() to iterate over the selectors list.
  • Apply operator.getitem() for each key in selectors, retrieving the value to be used as the iteratee for the next iteration.
from functools import reduce from operator import getitem def get(d, selectors): return reduce(getitem, selectors, d)
users = { 'freddy': { 'name': { 'first': 'fred', 'last': 'smith' }, 'postIds': [1, 2, 3] } } get(users, ['freddy', 'name', 'last']) # 'smith' get(users, ['freddy', 'postIds', 1]) # 2

python snippet similar to get For You in November 2022

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2022 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️