Quantcast
Channel: How to use a dot "." to access members of dictionary? - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Answer by arod for How to use a dot "." to access members of dictionary?

$
0
0

I just needed to access a dictionary using a dotted path string, so I came up with:

def get_value_from_path(dictionary, parts):""" extracts a value from a dictionary using a dotted path string """    if type(parts) is str:        parts = parts.split('.')    if len(parts) > 1:        return get_value_from_path(dictionary[parts[0]], parts[1:])    return dictionary[parts[0]]a = {'a':{'b':'c'}}print(get_value_from_path(a, 'a.b')) # c

Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>